<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Java_Native_Interface</id>
	<title>Java Native Interface - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki1463/index.php?action=history&amp;feed=atom&amp;title=Java_Native_Interface"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Java_Native_Interface&amp;action=history"/>
	<updated>2026-12-01T03:45:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.3</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki1463/index.php?title=Java_Native_Interface&amp;diff=157851&amp;oldid=prev</id>
		<title>imported&gt;Yodin: &quot;based off of&quot; → &quot;based on&quot;; clean up</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki1463/index.php?title=Java_Native_Interface&amp;diff=157851&amp;oldid=prev"/>
		<updated>2026-08-27T14:40:37Z</updated>

		<summary type="html">&lt;p&gt;&amp;quot;based off of&amp;quot; → &amp;quot;based on&amp;quot;; clean up&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Foreign function interface for the Java language}}&lt;br /&gt;
{{Redirect|JNI}}&lt;br /&gt;
{{multiple issues|&lt;br /&gt;
{{more citations needed|date=February 2017}}&lt;br /&gt;
{{lead too short|date=February 2017}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;Java Native Interface&amp;#039;&amp;#039;&amp;#039; (or Native Method Interface) is a [[foreign function interface]] designed for non-Java programming [[software framework|frameworks]]. The JNI enables [[Java (programming language)|Java]] code to call and be called by native code (i.e. any code using the &amp;lt;code&amp;gt;JNIEnv&amp;lt;/code&amp;gt; and interfacing through the C [[application binary interface]]), native applications (programs specific to a hardware and [[operating system]] platform) and [[Library (computing)|libraries]] written in other languages such as [[C (programming language)|C]], [[C++]] and [[assembly language|assembly]].&amp;lt;ref name=&amp;quot;role&amp;quot;&amp;gt;{{cite web |url=https://docs.oracle.com/en/java/javase/11/docs/specs/jni/intro.html#java-native-interface-overview |title=Java Native Interface Overview |access-date=2018-12-27 |work= The Java Native Interface Programmer&amp;#039;s Guide and Specification}}&lt;br /&gt;
&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|url=https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/intro.html|title=Java Native Interface Overview SE 7}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=https://developer.ibm.com/articles/j-jni/|title=Using the wrong JNIEnv}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Java 22 introduces the [[Java Native Interface#Foreign Function and Memory API|Foreign Function and Memory API]], which can be seen as the successor to Java Native Interface.&amp;lt;ref&amp;gt;{{Cite web|url=https://openjdk.org/projects/jdk/22/|title=Project Panama (JDK 22)}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=https://docs.oracle.com/en/java/javase/25/docs/specs/jni/intro.html|title=JNI Specification (Java SE 25)}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Objectives ==&lt;br /&gt;
JNI allows native methods to allow for applications to make calls outside of the Java platform, e.g. when the standard Java [[class (computer science)|class]] [[Library (computer science)|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 [[Java Class Library|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 [[Java applet|applets]] may invoke JNI.&lt;br /&gt;
&lt;br /&gt;
The JNI framework allows native code to manipulate and consume Java [[object (computer science)|objects]]. A native method (marked by the keyword &amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot; inline&amp;gt;native&amp;lt;/syntaxhighlight&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
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).&lt;br /&gt;
&lt;br /&gt;
Not only can native code interface with Java, it can also draw on a Java {{Javadoc:SE|java/awt|Canvas|module=java.desktop}}, which is possible with the [[Java AWT Native Interface]], available since [[Java Platform, Standard Edition|J2SE]] 1.3.&lt;br /&gt;
&lt;br /&gt;
JNI also allows direct access to [[Assembly language|assembly]], without a [[C (programming language)|C]] bridge.&amp;lt;ref&amp;gt;{{cite web | url = http://today.java.net/pub/a/today/2006/10/19/invoking-assembly-language-from-java.html | title = Invoking Assembly Language Programs from Java | publisher = Java.net | date = 2006-10-19 | access-date = 2007-10-06 | url-status = dead | archive-url = https://web.archive.org/web/20080330002620/http://today.java.net/pub/a/today/2006/10/19/invoking-assembly-language-from-java.html | archive-date = 2008-03-30 }}&amp;lt;/ref&amp;gt; Accessing Java applications from assembly is possible in the same way.&amp;lt;ref&amp;gt;{{ cite web | url = http://today.java.net/pub/a/today/2007/10/04/launch-java-applications-from-assembly-language-programs.html | title = Launch Java Applications from Assembly Language Programs | publisher = Java.net | date = 2006-10-19 | access-date = 2007-10-04 | url-status = dead | archive-url = https://web.archive.org/web/20071011014140/http://today.java.net/pub/a/today/2007/10/04/launch-java-applications-from-assembly-language-programs.html | archive-date = 2007-10-11 }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The JNI is the entry point between languages used to begin the JVM startup.&amp;lt;ref name=&amp;quot;JNI Specification&amp;quot;&amp;gt;{{cite web|url=https://docs.oracle.com/en/java/javase/20/docs/specs/jni/invocation.html|title=Creating the JVM}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=https://inside.java/2025/01/28/jvm-start-up/|title=A Deep Dive into JVM Start-up}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=https://github.com/openjdk/jdk/blob/85fedbf668023fd00d70ec649504c2f80e4c84bb/src/java.base/share/native/include/jni.h#L1985|title=Create JVM Header (source code)}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web|url=https://github.com/openjdk/jdk/blob/jdk-23%2B0/src/hotspot/share/prims/jni.cpp#L3661C1-L3661C93|title=Create JVM Implementation (source code)}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Mapping types ==&lt;br /&gt;
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 [[typedef]]s are provided:&amp;lt;ref&amp;gt;{{cite web|url=https://docs.oracle.com/en/java/javase/11/docs/specs/jni/types.html|title=JNI Types and Data Structures}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Java type&lt;br /&gt;
 ! Native typedef&lt;br /&gt;
 ! Description&lt;br /&gt;
 ! JVM type signature&lt;br /&gt;
 ! Closest native type&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;boolean&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jboolean&amp;lt;/code&amp;gt;&lt;br /&gt;
| unsigned 8 bits&lt;br /&gt;
| {{mono|Z}}&lt;br /&gt;
| &amp;lt;code&amp;gt;bool&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;byte&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jbyte&amp;lt;/code&amp;gt;&lt;br /&gt;
| signed 8 bits&lt;br /&gt;
| {{mono|B}}&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;int8_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jchar&amp;lt;/code&amp;gt;&lt;br /&gt;
| unsigned 16 bits&lt;br /&gt;
| {{mono|C}}&lt;br /&gt;
| &amp;lt;code&amp;gt;char16_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jshort&amp;lt;/code&amp;gt;&lt;br /&gt;
| signed 16 bits&lt;br /&gt;
| {{mono|S}}&lt;br /&gt;
| &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;int16_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jint&amp;lt;/code&amp;gt;&lt;br /&gt;
| signed 32 bits&lt;br /&gt;
| {{mono|I}}&lt;br /&gt;
| &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;int32_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jlong&amp;lt;/code&amp;gt;&lt;br /&gt;
| signed 64 bits&lt;br /&gt;
| {{mono|J}}&lt;br /&gt;
| &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;int64_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jfloat&amp;lt;/code&amp;gt;&lt;br /&gt;
| 32 bits&lt;br /&gt;
| {{mono|F}}&lt;br /&gt;
| &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;float32_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jdouble&amp;lt;/code&amp;gt;&lt;br /&gt;
| 64 bits&lt;br /&gt;
| {{mono|D}}&lt;br /&gt;
| &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;float64_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;void&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;void&amp;lt;/code&amp;gt;&lt;br /&gt;
| not applicable&lt;br /&gt;
| {{mono|V}}&lt;br /&gt;
| &amp;lt;code&amp;gt;void&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
In addition, the JVM [[type signature]] &amp;lt;code&amp;gt;L&amp;#039;&amp;#039;fully-qualified-class&amp;#039;&amp;#039;;&amp;lt;/code&amp;gt; refers the class uniquely specified by that name, for example the signature &amp;lt;code&amp;gt;Ljava.lang.String;&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;Ljava/lang/String;&amp;lt;/code&amp;gt; in [[Java class file|class files]]) refers to the class {{Javadoc:SE|package=java.lang|java/lang|String}}. Prefixing &amp;lt;code&amp;gt;[&amp;lt;/code&amp;gt; to the signature denotes an array of that type; for example, &amp;lt;code&amp;gt;[I&amp;lt;/code&amp;gt; refers to &amp;lt;code&amp;gt;int[]&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;[[I&amp;lt;/code&amp;gt; refers to &amp;lt;code&amp;gt;int[][]&amp;lt;/code&amp;gt;, up to a maximum of 255 dimensions.&lt;br /&gt;
&lt;br /&gt;
These types are interchangeable. One can use &amp;lt;code&amp;gt;jint&amp;lt;/code&amp;gt; where you normally use an &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;, and vice versa, without any [[type conversion|typecast]]ing required. However, mapping between Java strings and arrays to native strings and arrays is different; for instance, &amp;lt;code&amp;gt;jstring&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;char*&amp;lt;/code&amp;gt; are not interchangeable types, nor are &amp;lt;code&amp;gt;jintArray&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;int[]&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
JNI additionally contains the following declarations for Java reference types in &amp;lt;code&amp;gt;&amp;lt;jni.h&amp;gt;&amp;lt;/code&amp;gt;:&amp;lt;ref&amp;gt;{{Cite web|title=openjdk/jdk - jni.h|url=https://github.com/openjdk/jdk/blob/master/src/java.base/share/native/include/jni.h|author=OpenJDK|publisher=OpenJDK|website=github.com|date=13 November 2024}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
#ifdef __cplusplus&lt;br /&gt;
class _jobject {};&lt;br /&gt;
class _jclass : public _jobject {};&lt;br /&gt;
// ...&lt;br /&gt;
typedef _jobject *jobject;&lt;br /&gt;
typedef _jclass *jclass;&lt;br /&gt;
// ...&lt;br /&gt;
#else&lt;br /&gt;
struct _jobject;&lt;br /&gt;
// ...&lt;br /&gt;
typedef struct _jobject *jobject;&lt;br /&gt;
typedef jobject jclass;&lt;br /&gt;
// ...&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Here, all Java reference types are defined as opaque empty types which act as handles to Java objects. These types should not be dereferenced.&amp;lt;ref&amp;gt;{{Cite web|title=JNI Programming in C++|url=https://www.cis.upenn.edu/~bcpierce/courses/629/papers/Java-tutorial/native1.1/implementing/cpp.html|author=Benjamin Crawford Pierce|publisher=University of Pennsylvania|website=cis.upenn.edu|access-date=12 June 2026}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Java type&lt;br /&gt;
 ! JNI handle&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.Object&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jobject&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.Class&amp;lt;/code&amp;gt; (non-generic)&lt;br /&gt;
| &amp;lt;code&amp;gt;jclass&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.Throwable&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jthrowable&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.String&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jstring&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;T[]&amp;lt;/code&amp;gt; (for a type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;)&lt;br /&gt;
| &amp;lt;code&amp;gt;jarray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;boolean[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jbooleanArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;byte[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jbyteArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;char[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jcharArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;short[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jshortArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;int[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jintArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;long[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jlongArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;float[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jfloatArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;double[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jdoubleArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.Object[]&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;jobjectArray&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Additionally, the following opaque types are used as handles:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Java class meta-object&lt;br /&gt;
 ! JNI handle&lt;br /&gt;
|-&lt;br /&gt;
| Class field&lt;br /&gt;
| &amp;lt;code&amp;gt;jfieldID&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Class method&lt;br /&gt;
| &amp;lt;code&amp;gt;jmethodID&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Note however that &amp;lt;code&amp;gt;jfieldID&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;jmethodID&amp;lt;/code&amp;gt; are just opaque handles, and do not correspond to &amp;lt;code&amp;gt;java.lang.reflect.Field&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;java.lang.reflect.Method&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Design ==&lt;br /&gt;
The communication between native code and Java code is intermediated by the JNI, acting as an [[Application binary interface|ABI]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// jni.h file&lt;br /&gt;
&lt;br /&gt;
struct JNINativeInterface_;&lt;br /&gt;
struct JNIEnv_;&lt;br /&gt;
&lt;br /&gt;
#ifdef __cplusplus&lt;br /&gt;
typedef JNIEnv_ JNIEnv;&lt;br /&gt;
#else&lt;br /&gt;
typedef const struct JNINativeInterface_ *JNIEnv;&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;JNIEnv&amp;lt;/code&amp;gt; is used to access the structure of functions (array of function pointers), and each function being identifiable by a unique pointer.&amp;lt;ref&amp;gt;{{cite web|url=https://docs.oracle.com/en/java/javase/25/docs/specs/jni/design.html|title=JNI Design Overview}}&amp;lt;/ref&amp;gt; According to the programming language, C or C++, the implementation can be a little different.&amp;lt;ref&amp;gt;{{cite web|url=https://github.com/openjdk/jdk/blob/1cb4ef8581b5c5572474a5376baf4fd88c5ffeab/src/java.base/share/native/include/jni.h#L216|title=JNI source code}}&amp;lt;/ref&amp;gt; For example, in C:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// jni.h file&lt;br /&gt;
&lt;br /&gt;
struct JNINativeInterface_ {&lt;br /&gt;
&lt;br /&gt;
    jint (JNICALL *GetVersion) // JNICALL declaration of a pointer to identify the specific function;&lt;br /&gt;
       (JNIEnv *env); // Parameter env is a pointer to the JNIEnv pointer, the pointer-to-pointer;&lt;br /&gt;
&lt;br /&gt;
    jclass (JNICALL *DefineClass)&lt;br /&gt;
      (JNIEnv *env, const char *name);&lt;br /&gt;
&lt;br /&gt;
    jclass (JNICALL *FindClass)&lt;br /&gt;
      (JNIEnv *env, const char *name);&lt;br /&gt;
&lt;br /&gt;
    // Others functions pointers...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&lt;br /&gt;
In the JNI framework, native functions are implemented in separate {{mono|.c}} or {{mono|.cpp}} files (C++ provides a slightly simpler interface with JNI). When the JVM invokes the function, it passes a &amp;lt;code&amp;gt;JNIEnv*&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;jobject&amp;lt;/code&amp;gt;, and any Java arguments declared by the Java method. For example, the following converts &amp;lt;code&amp;gt;jstring&amp;lt;/code&amp;gt; to a &amp;lt;code&amp;gt;const char*&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
extern &amp;quot;C&amp;quot; {&lt;br /&gt;
&lt;br /&gt;
JNIEXPORT void JNICALL Java_ClassName_MethodName (JNIEnv* env, jobject obj, jstring javaString) {&lt;br /&gt;
    const char* nativeString = env-&amp;gt;GetStringUTFChars(javaString, 0);&lt;br /&gt;
&lt;br /&gt;
    // Do something with the nativeString&lt;br /&gt;
&lt;br /&gt;
    env-&amp;gt;ReleaseStringUTFChars(javaString, nativeString);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;env&amp;lt;/code&amp;gt; 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. Anything that Java code can do can be done using &amp;lt;code&amp;gt;JNIEnv&amp;lt;/code&amp;gt;. The argument &amp;lt;code&amp;gt;obj&amp;lt;/code&amp;gt; is a reference to the Java object inside which this native method has been declared.&lt;br /&gt;
&lt;br /&gt;
Native [[data type]]s can be mapped to/from Java data types. For compound types, such as objects, [[Array data structure|arrays]] and [[string (computer science)|string]]s, the native code must explicitly convert the data by calling methods in the &amp;lt;code&amp;gt;JNIEnv&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
A JNI environment pointer (&amp;lt;code&amp;gt;JNIEnv*&amp;lt;/code&amp;gt;) 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 &amp;lt;code&amp;gt;AttachCurrentThread()&amp;lt;/code&amp;gt; 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 &amp;lt;code&amp;gt;DetachCurrentThread()&amp;lt;/code&amp;gt; to detach itself.&amp;lt;ref&amp;gt;The Invocation API. Sun Microsystems. https://docs.oracle.com/en/java/javase/11/docs/specs/jni/invocation.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 code is responsible explicitly releasing all acquired resources.&lt;br /&gt;
&lt;br /&gt;
On [[Linux]] and Solaris platforms, if the native code registers itself as a signal handler, it can intercept signals intended for the JVM. A [[Chain-of-responsibility pattern|chain of responsibility]] can be used to allow native code to interoperate with the JVM. On Windows platforms, [[Microsoft-specific exception handling mechanisms#Structured Exception Handling|Structured Exception Handling (SEH)]] may be employed to wrap native code in SEH &amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt;/&amp;lt;code&amp;gt;catch&amp;lt;/code&amp;gt; blocks so as to capture machine (CPU/FPU) generated software interrupts (such as [[null pointer]] dereferences and divide-by-zero operations), and to handle these situations before the interrupt is propagated back up into the JVM (i.e. Java side code).&lt;br /&gt;
&lt;br /&gt;
The encoding used for the &amp;lt;code&amp;gt;NewStringUTF()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringUTFLength()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringUTFChars()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ReleaseStringUTFChars()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;GetStringUTFRegion()&amp;lt;/code&amp;gt; functions is &amp;quot;modified [[UTF-8]]&amp;quot;,&amp;lt;ref name=&amp;quot;jniModifiedUtf&amp;quot;&amp;gt;{{cite web|url=http://docs.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp16542|title=JNI Types and Data Structures}}&amp;lt;/ref&amp;gt; which is not valid UTF-8 for all inputs, but a different encoding. The [[null character]] (U+0000) and codepoints not on the [[Plane (Unicode)#Basic Multilingual Plane|Basic Multilingual Plane]] (greater than or equal to U+10000, i.e. those represented as surrogate pairs in [[UTF-16]]) are encoded differently in modified UTF-8. Many programs actually use these functions incorrectly and treat the UTF-8 strings returned or passed into the functions as standard UTF-8 strings instead of modified UTF-8 strings. Programs should use the &amp;lt;code&amp;gt;NewString()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringLength()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringChars()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ReleaseStringChars()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringRegion()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GetStringCritical()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReleaseStringCritical()&amp;lt;/code&amp;gt; functions, which use UTF-16LE encoding on little-endian architectures and UTF-16BE on [[Endianness|big-endian]] architectures, and then use a UTF-16 to UTF-8 conversion routine.{{original research inline|date=February 2017}}&lt;br /&gt;
&lt;br /&gt;
For example, the following demonstrates a native implementation of a Java method &amp;lt;code&amp;gt;HelloJNI::sayHello()&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;jni.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv* env, jobject obj) {&lt;br /&gt;
    printf(&amp;quot;Hello from C!\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, in Java, the function is called like so:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class HelloJNI {&lt;br /&gt;
    static {&lt;br /&gt;
        // &amp;quot;hello&amp;quot; refers to libhello.so on Linux&lt;br /&gt;
        System.loadLibrary(&amp;quot;hello&amp;quot;); // System.load() is other way to load a library.&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public native void sayHello();&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        new HelloJNI().sayHello();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Performance ==&lt;br /&gt;
JNI incurs considerable overhead and performance loss under certain circumstances:&amp;lt;ref name=&amp;quot;StackOverflow: What makes JNI calls slow?&amp;quot;&amp;gt;{{cite web|url=https://stackoverflow.com/a/7809300/1068537|title=Java: What makes JNI calls slow? |website=Stack Overflow}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* Function calls to JNI methods are expensive, especially when repeatedly calling a method.&lt;br /&gt;
* 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.&lt;br /&gt;
* A Java array may be copied for access in native code, and later copied back. The cost is linear in the size of the array.&lt;br /&gt;
* 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 native code invokes Java [[reflective programming|reflection]]. Signatures are specified in strings and queried from the JVM. This is both slow and error-prone.&lt;br /&gt;
* Java strings are objects and have length and are encoded. Accessing or creating a string may require an &amp;lt;math&amp;gt;O(n)&amp;lt;/math&amp;gt; copy.&lt;br /&gt;
&lt;br /&gt;
== Foreign Function and Memory API ==&lt;br /&gt;
Java 22 introduced the Foreign Function and Memory API, located in {{Javadoc:SE|package=java.lang.foreign|java/lang/foreign}} which can be seen as a sort of successor to the Java Native Interface.&amp;lt;ref name=&amp;quot;javaFFI&amp;quot;&amp;gt;{{cite web|url= https://openjdk.org/jeps/454|title= JEP 454: Foreign Function &amp;amp; Memory API|date=7 April 2025|publisher=OpenJDK}}&amp;lt;/ref&amp;gt; It has a simpler interface and generally requires less boilerplate than JNI, due to no longer requiring the inclusion of &amp;lt;code&amp;gt;&amp;lt;jni.h&amp;gt;&amp;lt;/code&amp;gt; in the foreign library to invoke, and Foreign Function and Memory API abstracts away the usage of the &amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot; inline&amp;gt;native&amp;lt;/syntaxhighlight&amp;gt; keyword completely. It supports [[region-based memory management]]. The class &amp;lt;code&amp;gt;java.lang.foreign.MemorySegment&amp;lt;/code&amp;gt; models a contiguous segment of memory which can lie inside or outside the JVM heap, and a &amp;lt;code&amp;gt;java.lang.foreign.MemorySegment&amp;lt;/code&amp;gt; is allocated using a &amp;lt;code&amp;gt;java.lang.foreign.Arena&amp;lt;/code&amp;gt; which controls the lifetime of the region of memory backing its allocated segment of memory.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;java.lang.foreign.Arena&amp;lt;/code&amp;gt; offers the following arena kinds:&amp;lt;ref&amp;gt;{{Cite web|title=Interface Arena|url=https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/Arena.html|author=Oracle Corporation|publisher=Oracle Corporation|website=docs.oracle.com|date=15 June 2026}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Arena kind&lt;br /&gt;
 ! Factory method&lt;br /&gt;
 ! Bounded lifetime&lt;br /&gt;
 ! Explicitly closeable&lt;br /&gt;
 ! Accessible from multiple threads&lt;br /&gt;
|-&lt;br /&gt;
| Global&lt;br /&gt;
| &amp;lt;code&amp;gt;Arena.global()&amp;lt;/code&amp;gt;&lt;br /&gt;
| No&lt;br /&gt;
| No&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Automatic&lt;br /&gt;
| &amp;lt;code&amp;gt;Arena.ofAuto()&amp;lt;/code&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| No&lt;br /&gt;
| Yes&lt;br /&gt;
|-&lt;br /&gt;
| Confined&lt;br /&gt;
| &amp;lt;code&amp;gt;Arena.ofConfined()&amp;lt;/code&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| No&lt;br /&gt;
|-&lt;br /&gt;
| Shared&lt;br /&gt;
| &amp;lt;code&amp;gt;Arena.ofShared()&amp;lt;/code&amp;gt;&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
| Yes&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
package org.wikipedia.examples;&lt;br /&gt;
&lt;br /&gt;
import java.lang.foreign.Arena;&lt;br /&gt;
import java.lang.foreign.MemorySegment;&lt;br /&gt;
import java.lang.foreign.ValueLayout;&lt;br /&gt;
&lt;br /&gt;
public class ForeignMemoryExample {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        try (Arena arena = Arena.ofConfined()) {&lt;br /&gt;
            MemorySegment segment = arena.allocate(5 * Double.BYTES);&lt;br /&gt;
            &lt;br /&gt;
            for (int i = 0; i &amp;lt; 5; ++i) {&lt;br /&gt;
                segment.setAtIndex(ValueLayout.JAVA_DOUBLE, i, i * 1.1);&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            for (int i = 0; i &amp;lt; 5; ++i) {&lt;br /&gt;
                double value = segment.getAtIndex(ValueLayout.JAVA_DOUBLE, i);&lt;br /&gt;
                System.out.printf(&amp;quot;Value at index %d: %d%n&amp;quot;, i, value);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In a sense, Java Foreign Function and Memory API allows for direct memory allocation outside the JVM heap (i.e. the operating system heap), through &amp;lt;code&amp;gt;Arena.allocate()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MemorySegment.allocateNative()&amp;lt;/code&amp;gt; which allocate raw memory directly, while &amp;lt;code&amp;gt;Arena.close()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MemorySegment.close()&amp;lt;/code&amp;gt; are used to deallocate/release the memory segment.&lt;br /&gt;
&lt;br /&gt;
As a [[foreign function interface]], the classes &amp;lt;code&amp;gt;java.lang.foreign.Linker&amp;lt;/code&amp;gt; is offered for accessing between foreign functions from Java (to and from), &amp;lt;code&amp;gt;java.lang.foreign.SymbolLookup&amp;lt;/code&amp;gt; for retrieving the address of a symbol in a library, and &amp;lt;code&amp;gt;java.lang.foreign.FunctionDescriptor&amp;lt;/code&amp;gt; for modelling a foreign function signature.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
package org.wikipedia.examples;&lt;br /&gt;
&lt;br /&gt;
import java.lang.foreign.Arena;&lt;br /&gt;
import java.lang.foreign.FunctionDescriptor;&lt;br /&gt;
import java.lang.foreign.Linker;&lt;br /&gt;
import java.lang.foreign.MemorySegment;&lt;br /&gt;
import java.lang.foreign.SymbolLookup;&lt;br /&gt;
import java.lang.foreign.ValueLayout;&lt;br /&gt;
import java.lang.invoke.MethodHandle;&lt;br /&gt;
&lt;br /&gt;
public class ForeignFunctionExample {&lt;br /&gt;
    public static void main(String[] args) throws Throwable {&lt;br /&gt;
        Linker linker = Linker.nativeLinker();&lt;br /&gt;
        &lt;br /&gt;
        SymbolLookup stdlib = linker.defaultLookup();&lt;br /&gt;
        &lt;br /&gt;
        MethodHandle printf = linker.downcallHandle(&lt;br /&gt;
            stdlib.findOrThrow(&amp;quot;printf&amp;quot;),&lt;br /&gt;
            FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS)&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        MethodHandle strlen = linker.downcallHandle(&lt;br /&gt;
            stdlib.findOrThrow(&amp;quot;strlen&amp;quot;),&lt;br /&gt;
            FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.ADDRESS)&lt;br /&gt;
        );&lt;br /&gt;
&lt;br /&gt;
        try (Arena arena = Arena.ofConfined()) {&lt;br /&gt;
            MemorySegment formatString = arena.allocateUtf8String(&amp;quot;Hello, %s!\n&amp;quot;);&lt;br /&gt;
            MemorySegment argString = arena.allocateUtf8String(&amp;quot;World&amp;quot;);&lt;br /&gt;
            printf.invokeExact(formatString, argString); // prints &amp;quot;Hello, World!&amp;quot;&lt;br /&gt;
            long len = (long)strlen.invokeExact(formatString); // len = 5&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Layouts are modelled by &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout&amp;lt;/code&amp;gt;, which consists of:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
 ! Type&lt;br /&gt;
 ! Name&lt;br /&gt;
 ! Description&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.AddressLayout&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;ADDRESS&amp;lt;/code&amp;gt;&lt;br /&gt;
| An address layout with the same size as [[size_t|&amp;lt;code&amp;gt;size_t&amp;lt;/code&amp;gt;]]&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.AddressLayout&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;ADDRESS_UNALIGED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned address layout with the same size as &amp;lt;code&amp;gt;size_t&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfBoolean&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_BOOLEAN&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;boolean&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfByte&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_BYTE&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;byte&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfChar&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_CHAR&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfChar&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_CHAR_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;char&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfShort&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_SHORT&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfShort&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_SHORT_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;short&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfInt&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_INT&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfInt&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_INT_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;int&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfLong&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_LONG&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfLong&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_LONG_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;long&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfFloat&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_FLOAT&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfFloat&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_FLOAT_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;float&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfDouble&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_DOUBLE&amp;lt;/code&amp;gt;&lt;br /&gt;
| A value layout constant with the same size as a Java &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;java.lang.foreign.ValueLayout.OfDouble&amp;lt;/code&amp;gt;&lt;br /&gt;
| &amp;lt;code&amp;gt;JAVA_DOUBLE_UNALIGNED&amp;lt;/code&amp;gt;&lt;br /&gt;
| An unaligned value layout with the same size as a Java &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Primitives receive &amp;lt;code&amp;gt;ValueLayout.JAVA_*&amp;lt;/code&amp;gt; layouts when mapped to their native types, while pointer types use &amp;lt;code&amp;gt;ValueLayout.ADDRESS&amp;lt;/code&amp;gt; (for instance &amp;lt;code&amp;gt;char*&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;int**&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;struct Point*&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
Additionally, the constant &amp;lt;code&amp;gt;MemorySegment.NULL&amp;lt;/code&amp;gt; (of type &amp;lt;code&amp;gt;java.lang.foreign.MemorySegment&amp;lt;/code&amp;gt;) is a zero-length native segment representing a null address (&amp;lt;code&amp;gt;nullptr&amp;lt;/code&amp;gt;), equivalent to &amp;lt;code&amp;gt;MemorySegment.ofAddress(0)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Java offers additional methods to express more complicated native types. For instance, the following native types&amp;lt;ref&amp;gt;{{Cite web|title=Interface Linker|url=https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/foreign/Linker.html|author=Oracle Corporation|publisher=Oracle Corporation|website=docs.oracle.com|date=15 June 2026}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
struct Point {&lt;br /&gt;
    int x;&lt;br /&gt;
    long y;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
union Choice {&lt;br /&gt;
    float a;&lt;br /&gt;
    int b;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
receive the following layouts:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.lang.foreign.MemoryLayout;&lt;br /&gt;
import java.lang.foreign.StructLayout;&lt;br /&gt;
import java.lang.foreign.UnionLayout;&lt;br /&gt;
&lt;br /&gt;
StructLayout pointLayout = MemoryLayout.structLayout(&lt;br /&gt;
    ValueLayout.JAVA_INT.withName(&amp;quot;x&amp;quot;),&lt;br /&gt;
    MemoryLayout.paddingLayout(4),&lt;br /&gt;
    ValueLayout.JAVA_LONG.withName(&amp;quot;y&amp;quot;)&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
UnionLayout choiceLayout = MemoryLayout.unionLayout(&lt;br /&gt;
    ValueLayout.JAVA_FLOAT.withName(&amp;quot;a&amp;quot;),&lt;br /&gt;
    ValueLayout.JAVA_INT.withName(&amp;quot;b&amp;quot;)&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Alternatives ==&lt;br /&gt;
The Java Runtime Interface was created by [[Netscape]] as a precursor.&amp;lt;ref&amp;gt;{{cite web|url=https://docs.oracle.com/en/java/javase/25/docs/specs/jni/intro.html#java-native-interface-overview|title=Historical Background}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Microsoft&amp;#039;s proprietary implementation of a Java Virtual Machine ([[Visual J++]]) had a similar mechanism for calling native code from Java, called the Raw Native Interface (RNI). In addition, it offered a simple way to call existing native code that was not itself aware of Java, such as (but not limited to) the [[Windows API]], (J/Direct). However, following the [[Visual J++#Litigation against J++|Sun–Microsoft litigation]] about this implementation, [[Visual J++]] ceased to be maintained. RNI was simpler to use than JNI, because no management of the Java environment pointer was needed; instead, all Java objects were accessed directly. To facilitate this, a tool was used to generate header files from Java classes. Similarly, J/Direct was easier to use than using the necessary intermediate native library and JNI.&lt;br /&gt;
&lt;br /&gt;
[[Java Native Access]] (JNA) is a community-developed library that provides Java programmers easy access to native shared libraries without using JNI. However, this requires the redistribution of the dependent jar library. The tradeoff is between JNI being more complex and JNA being slower, and not built-in like JNI.&amp;lt;ref&amp;gt;{{cite web |last1=Zakusylo |first1=Alexander |title=Github is also the original JNA source. JNA vs JNI benchmark speed tests |url=https://github.com/zakgof/java-native-benchmark |website=Github |publisher=Github |access-date=30 March 2023}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Historically, [[GNU Compiler Collection|GCC]] (until GCC 7) offered the [[GNU Compiler for Java]]. This compiler offered the [[GNU Compiler for Java#Compiled Native Interface|Compiled Native Interface]] (CNI), which allowed direct interoperability between [[C++]] and Java. It provided an API that was heavily based on JNI itself.&amp;lt;ref&amp;gt;{{Cite web|title=11.17 Reflection|url=https://gcc.gnu.org/onlinedocs/gcc-6.5.0/gcj/Reflection.html|publisher=GNU Project|author=GNU Project|access-date=22 May 2026|website=gcc.gnu.org}}&amp;lt;/ref&amp;gt; It claimed several advantages over JNI, as it represented Java classes directly as [[C++ classes]].&amp;lt;ref&amp;gt;[https://gcc.gnu.org/java/faq.html#2_3 The GCJ FAQ – GNU Project – Free Software Foundation (FSF)&amp;lt;!-- Bot generated title --&amp;gt;]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since Java 22, the Foreign Function and Memory API supersedes the JNI, due to its reduced boilerplate and simplified interface.&amp;lt;ref name=&amp;quot;javaFFI&amp;quot;/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[C++/CLI]]&lt;br /&gt;
* [[GIWS (software)]]&lt;br /&gt;
* [[GlueGen]]&lt;br /&gt;
* [[Java Access Bridge]]&lt;br /&gt;
* [[Java Debug Wire Protocol]]&lt;br /&gt;
* [[Java Native Access]]&lt;br /&gt;
* [[Java Virtual Machine Tools Interface]]&lt;br /&gt;
* [[GNU Compiler for Java]]&lt;br /&gt;
* [[libffi]]&lt;br /&gt;
* [[Platform Invocation Services]]&lt;br /&gt;
* [[Symbol table]]&lt;br /&gt;
* [[SWIG]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist|30em}}&lt;br /&gt;
&lt;br /&gt;
== Bibliography ==&lt;br /&gt;
{{refbegin}}&lt;br /&gt;
* {{cite book&lt;br /&gt;
| first = Rob&lt;br /&gt;
| last = Gordon&lt;br /&gt;
| date = March 1998&lt;br /&gt;
| title = Essential Jni: Java Native Interface&lt;br /&gt;
| publisher = [[Prentice Hall]]&lt;br /&gt;
| edition = 1st&lt;br /&gt;
| pages = 498&lt;br /&gt;
| isbn = 0-13-679895-0&lt;br /&gt;
| url = http://www.informit.com/store/product.aspx?isbn=9780136798958&lt;br /&gt;
}}&lt;br /&gt;
* {{cite book&lt;br /&gt;
| first = Sheng&lt;br /&gt;
| last = Liang&lt;br /&gt;
| date = June 20, 1999&lt;br /&gt;
| title = Java(TM) Native Interface: Programmer&amp;#039;s Guide and Specification&lt;br /&gt;
| publisher = [[Prentice Hall]]&lt;br /&gt;
| edition = 1st&lt;br /&gt;
| pages = 320&lt;br /&gt;
| isbn = 0-201-32577-2&lt;br /&gt;
| url = http://www.informit.com/store/product.aspx?isbn=9780201325775&lt;br /&gt;
}}&lt;br /&gt;
{{refend}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [https://docs.oracle.com/en/java/javase/25/docs/specs/jni/index.html Oracle&amp;#039;s JNI 25.0 API Specification]&lt;br /&gt;
* [https://web.archive.org/web/20120728074805/http://java.sun.com/docs/books/jni/ Java Native Interface: Programmer&amp;#039;s Guide and Specification]&lt;br /&gt;
* [https://web.archive.org/web/20111014002625/http://developer.apple.com/java/jniuniversal.html JNI in XCode from Apple]&lt;br /&gt;
* [http://www.codeproject.com/KB/debug/jni_ex.aspx Exception handling in JNI]&lt;br /&gt;
* [https://dev.java/learn/ffm/ The Foreign Function and Memory API Tutorial]&lt;br /&gt;
&lt;br /&gt;
[[Category:Java (programming language)]]&lt;br /&gt;
[[Category:Java (software platform)]]&lt;br /&gt;
&amp;lt;!-- Hidden categories below --&amp;gt;&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Yodin</name></author>
	</entry>
</feed>