HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Java Native Methods"
Vol 1, Issue 1, P.26


Listing 1

public class SimpleClass {
	int       anInteger;
	boolean   aBoolean;
	Object    anObject;

Listing 2

typedef struct ClassSimpleClass {
	long                      anInteger;
	/*boolean*/ long          aBoolean;
	struct Hjava_lang_Object *anObject;
} ClassSimpleClass;

Listing 3

typedef struct HSimpleClass {
	ClassSimpleClass    *obj;
	struct methodtable  *methods;
} HSimpleClass;

Listing 4

HObject *objList =
(HArrayOfObject *) ArrayAlloc(T_CLASS, len);
memset(unhand(objList)->body,0,len * sizeof(Hobject *));
// replace "java/lang/Object" below with the class
// you are storing in the array
struct ClassClass *clsObject = 
FindClass(0, "java/lang/Object", TRUE);
unhand(objList)->body[len] = (HObject *)clsObject;

Listing 5

public class SimpleClass2 {
	public native int add(int a, int b);
	public static void main(String[] args) {
		SimpleClass2 simple = new SimpleClass2();
		int sum = simple.add(2,3);
		System.out.println("The sum is "+sum);

Listing 6

#include <native.h>
#include "SimpleClass2.h"
long SimpleClass2_add(struct HSimpleClass2 *self, long a, long b) {
	return a + b;


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.