ðHgeocities.com/kongwenyu/jni.htmlgeocities.com/kongwenyu/jni.htmldelayedxàmÔJÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÈ e¿ù)OKtext/htmlП™ù)ÿÿÿÿb‰.HThu, 05 Oct 2000 18:36:21 GMTJMozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)en, *ßmÔJù) A Brief Introduction to Java Native Interface

A Brief Introduction to Java Native Interface

Over the past several years, the Java Programming language has gained considerable popularity from it's flexibility, portability, and relative simplisity(compare with other o-o progamming langauages). Due to this, many organizations are currently evaluating how to integrating Java efectively to their existing software processes. There are quite a lot chances during this process that require to integrate your Java codes with your company's legacy codes. Java Native Interface(JNI) is one of the solutions.

In this article I am trying to provide the reader with the following information in about 10 to 15 minutes reading: What is a java native method ? What is JNI? When should we think of to use it?. what kind of basic steps are involved to use it? Where can we find in-depth study material when we need to do the job?

Native methods are functions written in a language other than Java. it could be C, C++, or even assembly. These function implementation are compiled into a dynamic link library(on Solaris, called shared objects.(.os's) ; on Win32, called dynamic link libraries(dlls)), which the operating system magically loads and links into the process that is running the Java Virtual Machine.

JNI is part of the Java Developer Kit(JDK), It serves as a glue between java side and native side of an application. It provide all kinds of functionality for communication from both sides of an application. Also JDK provides utility tools to do the mapping (methods ptototype and data type)between the Java side and native side. The JNI functions are avalibale through an interface pointer, which is the first parameter of every native methods implementation on native side.

Now we know that Java provide JNI to interagting Java code and native code, but when should we think of to use it ? Since JNI add complexity to project development, and also there are other drawbacks.[1] [2]We should only use it when we have to. here are some situations, typically people will think of using JNI: Whenever some functionality are not provided by java; Whenever people try to use existing legacy library and could not afford to rewrite it in java; whenever performance is a big issue; whenever new development on an existing legacy application has migaration to other platform in mind. Also we need keep in mind that JNI is not the only solution to the java-native integration problem. It is really a case-by-case situation.[3][4]

Now let us see what are the basic steps are involved. On Java side the work involved with calling native method are pretty simple. first you need declare the native method in one of Java class, then use the loadLibrary method of System class to specify the library which comtain the native method implementation. The Java class defination could be as following:

	class sample{

		static{
	
			System.loadLibrary("sampleImp");
		}
	
		public native int sum(int[] a, int[] b);
		//rest of the class definition
	}
Here native is the Java keyward to mark that this method is implemented in a language other than Java. The call to this native method sum on Java side is just as the regular method call in Java.

Now we finish the work on Java side, let us move to the native side(here use C) to provide the implmentation for this native method sum of class Sample. This require us to resolve the native method sum's prototype which is used on the native C side. and also we need mapping the Java types(primitives and references) to native C types. This will be done for us by using a Java utility program javah . The usage of javah is as following: assume that the above class is defined in file sample.java. when you compile it with javac, you get sample.class. then on the command line, you type javah sample , this will generate a header file called sample.h that defines an ANSI C function prototype for each native method declared in sample.java. For our example here, you can find the prototype for native method sum as the following:

    JNIEXPORT jint JNICALL Java_sample_sum(JNIEnv *, jobject, jintArray, jintArray);
	

Here JNIEXPORT and JNICALL are two platform-dependent #define marcos. also please note that two extra arguments are added to the argument list of method sum. The first argument is called interface pointer which can be used by native function to access JNI library function. The second argument jobejct is a reference to the caller object from Java side. In a sense that you can think of jobject as this keyword in Java. These two arguments are added to every native function by javah utility program to function as a glue between Java side and native side. Finally you may notice that method name and argument type are changed. sum is mapped to Java_sample_sum. "int" is mapped to jint and int[](Java integer type array) is mapped to jintArray. The mapping is done by "javah" following the Java Name Mapping Convention .

The last two things to do are filling in the functionality for Java_sample_sum which you need to write in native code and compiling the source file to a shared library. For an experienced native language programmer, the only things new are mainly the following three kinds: How to deal with the native type such as jintArray ? This will be needed when we process the data passed through argument list. For above example we may need get the element value inside of the array of type jintArray. Refer to [5] [6] [7] for more information on this topic. The second one is how to get information from Java side ? or we can say how to access Java object, it's member variables and methods. Refer to [8] for details about how to access Java object member variables. As for accessing Java object methods, Please refer to [9]. After we know how to access Java side information and deal with the native type, we can process the information and finally we need wrap the information back to native type and pass back to Java side. The JNI library provide all knids of function to do this. Most of these kind of function has a prefix "New".[7] Finally for building a shared library,[9] the shared library name must be "libsampleImp.so" on Solaris for above example. This is very important.

So the steps involved are the following: (1) Define your java class with one or more native methods. (2) compile your Java class (3) run javah on your java class to get the header file (4) use the prototypes in the header file to implement your native function (5) compile your native code to create shared library (6) Run your programe.

There are two kind of integrating cases. One is integrating native code to an application running on JVM. The other is integrating Java code to a native application running outside of the JVM. For the later case, we need to create a JVM so that the native application could be able to load and process Java class. JNI provide the ability for a native application to create a JVM. This part of the JNI is called Invocation API , Please refer to [10][4][11] for more detail about this topic.

In this article, I introduced the basic steps involved to use JNI to integrating Java and native code. For more advanced topic such as Exception handling, local and global object reference and threading, please refer to the reference books for more details. Also in GeoQuest, Mohammed Rupawalla's group already integrated six editors wrriten in Java to GeoFrame application. They have their lesson learned documented in [3].

Java is great, but the fact is that the other native languages have been there for so long. there are enormous invaluable native applications. It's not going to be an over night event to see a pure Java world. Java and other native languages are going to be coexisting for a long time. I will be more than happy to witness what eventually things are going to be.

Reference:

[3] GeoFrame 4.0 wu_wdm/gf40_OpenSpiritDMs_Lessons.fm
[4] "Essential JNI "by Rob Gordon
[11] "Java Native Interface "by Sheng Liang