October 1, 2002 Introduction Java's appearance brings great convenience to you. However, if we have a large number of originally tested non-Java code, we have rewritten all of them with Java, I am afraid it will bring huge workload and long-term testing; if we need to access a specific device in our application Even only the company's internal information interaction specification, or a specific operating system is characteristic, Java seems to be a bitter. In the face of these issues, Sun has defined JNI specification in JDK1.0, which specifies the Java application to call rules for local methods. 2 Implementation Steps and Related Functions Use this article to explain the local shared library with Java cooperation in the Linux platform. The Hello World program is the first step in the current standard, then I also use similar applications. The first step is to define a Java class - Hello. It provides a SayHello method: At this point, pay attention to two points: 1. Write the local method to declare the local method to use, and its declaration is nothing different from the ordinary Java method interface. Just specify the Native keyword, as shown below: Public native void selfhello (String Strname); In this function, we will say to someone according to the visual name. 2. The local code base must be explicitly loaded. We need to load this library in a static block in the class: static {system.loadLibrary ("Hello");} Add the necessary exception handling to generate the following source file hello.java: public class hello {static {Try {/ / Here is the link library name System.LoadLibrary ("Hello") in the local method;} catch (unsatisfiedlinker e) {system.err.println ("Cannot Load Hello Library: / N" E.TOSTRING ()); }}} Public hello () {} // The local method of the declaration public native void sayhello (String Strname);} Generates a hello.class file after compiling. In the second step, the local link library is generated.
The specific process is as follows: 1. To generate Java local interface files for the above defined class, you need to use Javah, the Java compiler's Javah feature will generate the necessary declaration according to the Hello class. This command will generate a Hello.h file, we share In the library, you want to include it. Javah does not make the default internal command, it needs to specify the path, it is in the JDK's bin directory, in my Linux environment command as follows: /Home/jbuilder/jdk1.3.1/bin/javah Hello generated The Hello.h file is as follows: / * do not edit this file - it is machine generated * / #include
Parameters: String Java String Object IsCopy If a copy, point to JNI_TRUE filling JBoolean, pointing to JNI_FALSE-filled JBoolean. Void ReleaseStringutfchars (JString Str, Const Char * Chars) Notification Virtual Machine Local Code No longer needs to access Java strings via Chars. Parameters: String Java String Object Chars Charl The pointer returned by getStringChars returns a new Java string and copy the UTF content into a new string, if the string object cannot be created, return NULL. It is usually used when the inverse value type is String. Parameters: UTF UTF encoded string pointer to the numerical parameters, can be used directly in C / C , the byte width is as follows: Java C / C bytes Boolean JBoolean 1 Byte JByte 1 Char Jchar 2 Short Jshort 2 int jint 4 long jlong 8 float jfloat 4 double jdouble 8 for array parameters, Java C / C boolean [] JbooleanArray byte [] JbyteArray char [] JcharArray short [] JshortArray int [] JintArray long [] JlongArray float [] JfloatArray double [] JDOUBLEARRAY has a set of functions to correspond to the above types. XXX is the corresponding type in the following functions. XXX * getxxxArrayElements (XXXARRAY ARRAY, JBOOLEAN * iScopy) generates a C pointer to Java array elements. This pointer is required to pass this pointer to ReleasexxaRrayElemes when it is not necessary. Parameters: Array array object iScopy If a copy is performed, pointing to JNI_TRUE populated with JNI_true, pointing JBoolean filled with jni_false. For example: JBoolean * getBooleanArrayElements (JBooleanArray Array, jboolean * iscopy) Void ReleasexxxArrayElements (XXXARRAY, XXX * ELEMS, JINT MODE) Notification The virtual machine no longer needs to get from getxxxaRrayElements. Parameters: Array array object ELEMS no longer needs to point to array elements Mode 0 = Release ELEMS buffer JNI_Commit = After updating array elements, do not release ELEMS buffer JNI_ABORT = no update array element release ELEMS buffer, for example : Void ReleaseBooleanArrayElements (JBooleanArray Array, JBoolean * elems, jint mode) XXXArray NewxxxArray (JSIZE LEN) generates a new array, usually used in the argument of the inverse value type as an array type: the number of elements in the LEN array. For example: JBooleanArray NewBooleanArray (JSIZE LEN) 3. Compile the generated shared library.