The Java Native Interface (or Native Method Interface) is a foreign function interface designed for non-Java programming frameworks. The JNI enables Java code to call and be called by native code (i.e. any code using the JNIEnv and interfacing through the C application binary interface), native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly. Java 22 introduces the Foreign Function and Memory API, which can be seen as the successor to Java Native Interface.
Objectives JNI allows native methods to allow for applications to make calls outside of the Java platform, e.g. when the standard Java class library does not support platform-specific features or a certain program library. It is also used to modify an existing application (written in another programming language) to be accessible to Java applications. Many standard library classes depend on JNI to provide functionality to the developer and the user, such as file I/O and audio APIs. Only applications and signed applets may invoke JNI. The JNI framework allows native code to manipulate and consume Java objects. A native method (marked by the keyword native in Java) can create Java objects and inspect and use these objects to perform its tasks. A native method can also inspect and use objects created by Java application code. An application that relies on JNI loses the platform portability Java offers (a partial workaround is to write a separate implementation of JNI code for each platform and have Java detect the operating system and load the correct one at runtime). Not only can native code interface with Java, it can also draw on a Java java.awt.Canvas, which is possible with the Java AWT Native Interface, available since J2SE 1.3. JNI also allows direct access to assembly, without a C bridge. Accessing Java applications from assembly is possible in the same way. The JNI is the entry point between languages used to begin the JVM startup.
Mapping types Java types are the default primitive types available on Java, while JVM type signatures are identifiers used by the JVM to identify the type matching on Java, while native types are types defined by the JNI itself to map to native types. For primitives, the following typedefs are provided:
In addition, the JVM type signature Lfully-qualified-class; refers the class uniquely specified by that name, for example the signature Ljava.lang.String; (Ljava/lang/String; in class files) refers to the class java.lang.String. Prefixing [ to the signature denotes an array of that type; for example, [I refers to int[], and [[I refers to int[][], up to a maximum of 255 dimensions. These types are interchangeable. One can use jint where you normally use an int, and vice versa, without any typecasting required. However, mapping between Java strings and arrays to native strings and arrays is different; for instance, jstring and char* are not interchangeable types, nor are jintArray and (C/C++) int[]. JNI additionally contains the following declarations for Java reference types in <jni.h>:
Here, all Java reference types are defined as opaque empty types which act as handles to Java objects. These types should not be dereferenced.
Additionally, the following opaque types are used as handles:
Note however that jfieldID and jmethodID are just opaque handles, and do not correspond to java.lang.reflect.Field/java.lang.reflect.Method. In GCC's Compiled Native Interface, which allows Java code to interface directly into C++, rather than using opaque handles the Java reference types are translated directly as pointers to those Java objects.
Design The communication between native code and Java code is intermediated by the JNI, acting as an ABI.
JNIEnv is used to access the structure of functions (array of function pointers), and each function being identifiable by a unique pointer. According to the programming language, C or C++, the implementation can be a little different. For example, in C:
Implementation 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 a JNIEnv*, a jobject, and any Java arguments declared by the Java method. For example, the following converts jstring to a const char*:
… excerpt ends here. Continue reading the full article.