Java Native Access
Java Native Access is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.
Since Java 22, the Foreign Function and Memory API was provided as a standard modern alternative.
Architecture
The JNA library uses a small native library called foreign function interface library to dynamically invoke native code. The JNA library uses native functions allowing code to load a library by name and retrieve a pointer to a function within that library, and uses libffi library to invoke it, all without static bindings, header files, or any compile phase. The developer uses a Java interface to describe functions and structures in the target native library. This makes it quite easy to take advantage of native platform features without incurring the high development overhead of configuring and building JNI code.JNA is built and tested on macOS, Microsoft Windows, FreeBSD / OpenBSD, Solaris, Linux, AIX, Windows Mobile, and Android. It is also possible to tweak and recompile the native build configurations to make it work on most other platforms that run Java.
Mapping types
The following table shows an overview of types mapping between Java and native code and supported by the JNA library.| Native Type | Size | Java Type | Common Windows Types |
| [Character (computing)|] | 8-bit integer | ||
| [Short integer|] | 16-bit integer | [Short integer|] | |
| [Wide character|] | 16/32-bit character | [Character (computing)|] | |
| [Integer (computer science)|] | 32-bit integer | [Integer (computer science)|] | |
| [Integer (computer science)|] | boolean value | ||
| [Long integer|] | 32/64-bit integer | ||
| long long | 64-bit integer | [Long integer|] | |
| [single precision|] | 32-bit FP | [single precision|] | |
| [double precision|] | 64-bit FP | [double precision|] | |
| C string | |||
Note: The meaning of changes between and according to some preprocessor definitions. follows.
Memory byte alignment for data structures
Native libraries have no standardized memory byte alignment flavor. JNA defaults to an OS platform specific setting, that can be overridden by a library specific custom alignment. If the alignment details are not given in the documentation of the native library, the correct alignment must be determined by trial and error during implementation of the Java wrapper.Example
The following program loads the local C standard library implementation and uses it to call the ' function.Note: The following code is portable and works the same on Windows and POSIX platforms.
package org.wikipedia.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
// Simple example of native library declaration and usage.
public class HelloWorld
The following program loads the C POSIX library and uses it to call the standard ' function.
Note: The following code is portable and works the same on POSIX standards platforms.
package org.wikipedia.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
// Simple example of native C POSIX library declaration and usage.
public class Example
The program below loads the and uses it to call the ' and ' functions.
Note: The following code works only on Windows platforms.
package org.wikipedia.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
// Simple example of Windows native library declaration and usage.
public class Example