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 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 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 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. Signatures are specified in strings and queried from the JVM. This is both slow and error-prone.
* Java Strings are objects, have length and are encoded. Accessing or creating a string may require an O(n) copy.
Alternatives
Microsoft's proprietary implementation of a Java Virtual Machine (Visual J++
Visual J++ is Microsoft's discontinued implementation of Java. Syntax, keywords, and grammatical conventions were the same as Java's. It was introduced in 1996 and discontinued in January 2004, replaced to a certain extent by J# and C#.
The ...
) had a similar mechanism for calling native code from Java, called the ''Raw Native Interface'' (''RNI''). In addition, it had an easy way to call existing native code that wasn't itself aware of Java, such as (but not limited to) the Windows API, called ''J/Direct''. However, following the Sun–Microsoft litigation about this implementation, Visual J++
Visual J++ is Microsoft's discontinued implementation of Java. Syntax, keywords, and grammatical conventions were the same as Java's. It was introduced in 1996 and discontinued in January 2004, replaced to a certain extent by J# and C#.
The ...
is no longer maintained.
RNI was less clumsy to use than JNI, because no bookkeeping with a Java environment pointer was needed. Instead, all Java objects could be accessed directly. To facilitate this, a tool was used that generated header files from Java classes. Similarly, J/Direct was easier to use than using the necessary intermediate native library and JNI.
Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using JNI.
See also
* GIWS (software)
GIWS is a wrapper generator intended to simplify calling Java from C or C++ by automatically generating the necessary JNI code.
GIWS is released under the CeCILL license.
Example
The following Java class does some simple computation.
p ...
* Gluegen {{Infobox software
, name = GlueGen
, logo =
, screenshot =
, caption =
, developer = Sun Microsystems Game Technology Group
JogAmp Community
* Platform Invocation Services
Platform Invocation Services, commonly referred to as P/Invoke, is a feature of Common Language Infrastructure implementations, like Microsoft's Common Language Runtime, that enables managed code to call native code.
Managed code, such as C# o ...
* SWIG
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other langu ...
References
Bibliography
*
*
{{refend
External links
Oracle's JNI 6.0 API Specification
Java Native Interface: Programmer's Guide and Specification
Exception handling in JNI
Java Link
(modern C++17 wrapper for JNI)
Java platform