Presentation is loading. Please wait.

Presentation is loading. Please wait.

JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim.

Similar presentations


Presentation on theme: "JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim."— Presentation transcript:

1 JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim

2 JNI: Java Native Interface(2/32) Real Time Systems LAB. Agenda JNI Types and Functions –Accessing Java Strings in Native methods –Accessing Java Arrays in Native methods –Calling Java Methods Conclusion

3 JNI: Java Native Interface(3/32) Real Time Systems LAB. JNI Types and Functions Mapping between JNI and Native Types –References Types –Primitive Types and Native equivalents –Encoding for Java Type signatures Handling Functions –JNI String –JNI Array –JNI Method –JNI Member Variable –JNI Exception –JNI Local and Global Reference JNI Thread Synchronization Functions

4 JNI: Java Native Interface(4/32) Real Time Systems LAB. Mapping between JNI and Native Types(1) JNI References Types

5 JNI: Java Native Interface(5/32) Real Time Systems LAB. Mapping between JNI and Native Types(2)

6 JNI: Java Native Interface(6/32) Real Time Systems LAB. Mapping between JNI and Native Types(3)

7 JNI: Java Native Interface(7/32) Real Time Systems LAB. Accessing Java Strings in Native methods The JNI supports the conversion to...native Unicode and UTF-8 strings If your code tries to print a jstring directly, it'll result in a VM crash!!! JNI functions for Accessing Java Strings –GetStringUTFChars converts unicode representation of a java string into a UTF-8 string, then can directly pass the string to regular C language functions, such as “printf” In Java, UTF-8 strings are always 0 terminated –ReleaseStringUTFChars When finish using the UTF-8 string, must call this function Then VM can free the memory taken by the UTF-8 string –NewString Constructs a new java.lang.String object from an array of Unicode characters –GetStringUTFLength Returns the length of a string, represented in the UTF-8 format

8 JNI: Java Native Interface(8/32) Real Time Systems LAB. Declaring Native Methods Mapping Between Java and Native types

9 JNI: Java Native Interface(9/32) Real Time Systems LAB. Using the JNIEnv Interface Pointer In native method, using the ‘env’ pointer to reference the JNI function Access and manipulate Java objects, such as strings, throuth the env interface pointer

10 JNI: Java Native Interface(10/32) Real Time Systems LAB. JNI String Handling Functions GetStringChars : the Unicode GetStringUTFChars : the UTF-8 format GetStringLength GetStringUTFLength NewString NewStringUTF ReleaseStringChars ReleaseStringUTFChars

11 JNI: Java Native Interface(11/32) Real Time Systems LAB. Example in Java side

12 JNI: Java Native Interface(12/32) Real Time Systems LAB. Example in Native code side

13 JNI: Java Native Interface(13/32) Real Time Systems LAB. Example in JNI header file Prompt.h

14 JNI: Java Native Interface(14/32) Real Time Systems LAB. Accessing Java Arrays in Native methods(1) The JNI uses the jarray type to represent references to Java arrays Similar to jstring, can’t directly access jarray types in native method C code JNI functions provide that to allow obtain pointers to the individual elements of (integer…) arrays –To get the length of the array (GetArrayLength) –To obtain a pointer a pointer to the individual elements of the array, then, can retrieve the elements (GetIntArrayElements) –To release the array memory (ReleaseIntArrayElements)

15 JNI: Java Native Interface(15/32) Real Time Systems LAB. Accessing Java Arrays in Native methods(2) Get Length –To get the length of the array –Carry length information of java arrays Get ArrayElements –To obtain a pointer to the elements of the array –Then, can retrieve the elements –Once, obtain the pointer, can use normal C language operations Release ArrayElements –To release array memory Get ArrayRegion & Set ArrayRegion –To accessing, via copying, a Small set of elements in an array GetObjectArrayElement & SetObjectArrayElement –To access elements of object arrays –To get & set individual object array elements –Can’t get all the object array elements at once!!

16 JNI: Java Native Interface(16/32) Real Time Systems LAB. JNI Array Handling Functions GetArrayLength Get ArrayElements –GetBooleanArrayElements –GetByteArrayElements –GetCharArrayElements –GetDoubleArrayElements –GetFloatArrayElements –GetIntArrayElements –GetLongArrayElements –GetShortArrayElements Release ArrayElements Get ArrayRegion Set ArrayRegion GetObjectArrayElement SetObjectArrayElement

17 JNI: Java Native Interface(17/32) Real Time Systems LAB. Example in Java side

18 JNI: Java Native Interface(18/32) Real Time Systems LAB. Example in Native code side

19 JNI: Java Native Interface(19/32) Real Time Systems LAB. Calling Java Methods Shows calling native language functions from a Java applications Shows how a legacy C program can use the JNI to link with Java libraries, call Java methods, use Java classes, etc

20 JNI: Java Native Interface(20/32) Real Time Systems LAB. Calling a Java Method from Native Code The JNI supports a complete set of “callback” operations that allow you to invoke a Java method from the Native code –Locate the method using method name & method signature –Can also invoke both class & instance methods –Use the ‘javap’ tool to generate JNI style method signatures from class files

21 JNI: Java Native Interface(21/32) Real Time Systems LAB. Instance method invocation Call an instance method by three steps 1.JNI function “GetObjectClass” Returns the Java class object that’s the type of the Java object 2.JNI function “GetMethodID” Performs a lookup for the Java method in a given class The lookup is based on the name of the method as well as the method signature If the method doesn’t exit, ‘GetMethodID’ returns ‘0’, immediately return from the native method at that point causes a ‘NoSuchMethodError’ to be thrown in the Java application code 3.JNI function “CallVoidMethod” Invokes an instance method that has ‘void’ return type, pass the ‘object’, ‘methodID’, & the ‘actual arguments’ to CallVoidMethod

22 JNI: Java Native Interface(22/32) Real Time Systems LAB. Forming the Method Name and Method Signature(1) The JNI performs a symbolic lookup based on the methods’s name & type signature Method type signature –“(argument-types)return-type”

23 JNI: Java Native Interface(23/32) Real Time Systems LAB. Forming the Method Name and Method Signature(2) Using ‘javap’ tool to Generate Method Signatures –To print out member variables and method signatures for specified classes –‘javap –s –p Prompt’ –‘-s’ flag : to generate the member variable signatures from class files –‘-p’ flag : to include private members –The method name is the Java method name in UTF-8 form –Specify the constructor of a class as “ ”

24 JNI: Java Native Interface(24/32) Real Time Systems LAB. JNI Method Handling Functions GetObjectClass GetMethodID GetStaticMethodID Call Method –CallBooleanMethod –CallByteMethod –CallCharMethod –CallDoubleMethod –CallFloatMethod –CallIntMethod –CallLongMethod –CallObjectMethod –CallShortMethod –CallVoidMethod CallStatic Method CallNonvirtual Method Call MethodV Call MethodA

25 JNI: Java Native Interface(25/32) Real Time Systems LAB. Example in Java side Accessing Java Member Variables

26 JNI: Java Native Interface(26/32) Real Time Systems LAB. Example in Native code side(1) To ‘get’ & ‘set’ Java member variables from native language method

27 JNI: Java Native Interface(27/32) Real Time Systems LAB. Example in Native code side(1) To ‘get’ & ‘set’ Java member variables from native language method

28 JNI: Java Native Interface(28/32) Real Time Systems LAB. JNI Member Variable Handling Functions GetFieldID GetStaticFieldID Get Field –GetBooleanField –GetByteField –GetCharField –GetDoubleField –GetFloatField –GetIntField –GetLongField –GetObjectField –GetShortField Set Field GetStatic Field SetStatic Field

29 JNI: Java Native Interface(29/32) Real Time Systems LAB. JNI Exception Handling Functions ExceptionClear ExceptionDescribe ExceptionOccurred

30 JNI: Java Native Interface(30/32) Real Time Systems LAB. JNI Local and Global Reference Handling Functions NewGlobalRef DeleteGlobalRef DeleteLocalRef

31 JNI: Java Native Interface(31/32) Real Time Systems LAB. JNI Thread Synchronization Functions MonitorEnter MonitorExit

32 JNI: Java Native Interface(32/32) Real Time Systems LAB. Conclusion Consideration of JDK upper version’s JNI improvements JNI Thread Synchronization Invoking the JVM into a native application


Download ppt "JNI: Java Native Interface(1/32) Real Time Systems LAB. JNI: Java Native Interface October 31, 2001 Kyung-Yim, Kim."

Similar presentations


Ads by Google