Objectives
JNI enables programmers to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e.g. when the standard JavaDesign
In the JNI framework, native functions are implemented in separate .c or .cpp files. (C++ provides a slightly simpler interface with JNI.) When the JVM invokes the function, it passes aJNIEnv
pointer, a jobject
pointer, and any Java arguments declared by the Java method. For example, the following converts a Java string to a native string:
env
'' pointer is a structure that contains the interface to the JVM. It includes all of the functions necessary to interact with the JVM and to work with Java objects. Example JNI functions are converting native arrays to/from Java arrays, converting native strings to/from Java strings, instantiating objects, throwing exceptions, etc. Basically, anything that Java code can do can be done using JNIEnv
, albeit with considerably less ease.
The argument ''obj
'' is a reference to the Java object inside which this native method has been declared.
Native data types can be mapped to/from Java data types. For compound types such as objects, JNIEnv
.
A JNI environment pointer () is passed as an argument for each native function mapped to a Java method, allowing for interaction with the JNI environment within the native method. This JNI interface pointer can be stored, but remains valid only in the current thread. Other threads must first call to attach themselves to the VM and obtain a JNI interface pointer. Once attached, a native thread works like a regular Java thread running within a native method. The native thread remains attached to the VM until it calls to detach itself.The Invocation API. Sun Microsystems. https://docs.oracle.com/en/java/javase/11/docs/specs/jni/invocation.html
The JNI framework does not provide any automatic garbage collection for non-JVM memory resources allocated by code executing on the native side. Consequently, native side code (such as assembly language) assumes the responsibility for explicitly releasing any such memory resources that the native code acquires.
On Linux and Solaris platforms, if the native code registers itself as a signal handler, it could intercept signals intended for the JVM. A Mapping types
The following table shows the mapping of types between Java (JNI) and native code. In addition, the signature"L qualified-class ;"
would mean the class uniquely specified by that name; e.g., the signature "Ljava/lang/String;"
refers to the class java.lang.String
. Also, prefixing _to_the_signature_makes_the_array_of_that_type;_for_example,_[I
_means_the_int_array_type._Finally,_a_void
_signature_uses_the_V
_code.
These_types_are_interchangeable._One_can_use_jint
_where_you_normally_use_an_int
,_and_vice_versa,_without_any_type_conversion">typecast_
In_film,_television,_and_theatre,_typecasting_is_the_process_by_which_a_particular_actor_becomes_strongly_identified_with_a_specific_character,_one_or_more_particular_roles,_or_characters_having_the_same__traits_or_coming_from_the_same_social_or__...
ing_required._However,_mapping_between_Java_Strings_and_arrays_to_native_strings_and_arrays_is_different._If_a_jstring
_is_used_where_a_char_*
_would_be,_the_code_could_crash_the_JVM.
__Performance_
JNI_incurs_considerable_overhead_and_performance_loss_under_certain_circumstances:
*_Function_calls_to_JNI_methods_are_expensive,_especially_when_calling_a_method_repeatedly.
*_Native_methods_are_not_inlined_by_the_JVM,_nor_can_the_method_be__to_the_signature_makes_the_array_of_that_type;_for_example,_[I
_means_the_int_array_type._Finally,_a_void
_signature_uses_the_V
_code.
These_types_are_interchangeable._One_can_use_jint
_where_you_normally_use_an_int
,_and_vice_versa,_without_any_type_conversion">typecast_
In_film,_television,_and_theatre,_typecasting_is_the_process_by_which_a_particular_actor_becomes_strongly_identified_with_a_specific_character,_one_or_more_particular_roles,_or_characters_having_the_same__traits_or_coming_from_the_same_social_or__...
ing_required._However,_mapping_between_Java_Strings_and_arrays_to_native_strings_and_arrays_is_different._If_a_jstring
_is_used_where_a_char_*
_would_be,_the_code_could_crash_the_JVM.
__Performance_
JNI_incurs_considerable_overhead_and_performance_loss_under_certain_circumstances:
*_Function_calls_to_JNI_methods_are_expensive,_especially_when_calling_a_method_repeatedly.
*_Native_methods_are_not_inlined_by_the_JVM,_nor_can_the_method_be_Just-in-time_compilation">JIT_compiled,_as_the_method_is_already_compiled.
*_A_Java_array_may_be_copied_for_access_in_native_code,_and_later_copied_back._The_cost_can_be_linear_in_the_size_of_the_array.
*_If_the_method_is_passed_an_object,_or_needs_to_make_a_callback,_then_the_native_method_will_likely_be_making_its_own_calls_to_the_JVM._Accessing_Java_fields,_methods_and_types_from_the_native_code_requires_something_similar_to_Reflection_(computer_programming).html" "title="Just-in-time_compilation.html" ;"title="type_conversion.html" "title="/code> to the signature makes the array of that type; for example, [I
means the int array type. Finally, a void
signature uses the V
code.
These types are interchangeable. One can use jint
where you normally use an int
, and vice versa, without any type conversion">typecast
In film, television, and theatre, typecasting is the process by which a particular actor becomes strongly identified with a specific character, one or more particular roles, or characters having the same traits or coming from the same social or ...
jstring
is used where a char *
would be, the code could crash the JVM.
Performance
JNI incurs considerable overhead and performance loss under certain circumstances: * Function calls to JNI methods are expensive, especially when calling a method repeatedly. * Native methods are not inlined by the JVM, nor can the method be Just-in-time compilation">JIT compiled, as the method is already compiled. * A Java array may be copied for access in native code, and later copied back. The cost can be linear in the size of the array. * If the method is passed an object, or needs to make a callback, then the native method will likely be making its own calls to the JVM. Accessing Java fields, methods and types from the native code requires something similar to Reflection (computer programming)">reflection Reflection or reflexion may refer to: Science and technology * Reflection (physics), a common wave phenomenon ** Specular reflection, reflection from a smooth surface *** Mirror image, a reflection in a mirror or in water ** Signal reflection, in ...Alternatives
Microsoft's proprietary implementation of a Java Virtual Machine (See also
* GIWS (software) *References
Bibliography
* * {{refendExternal links