Java Native Access
Page Module:Infobox/styles.css has no content.
| Java Native Access | |
|---|---|
| [[Programmer|Original authorTemplate:Pluralize from text]] | Todd Fast, Timothy Wall, Liang Chen |
| Initial release | Script error: No such module "Date time". |
| Written in | C and Java |
| Operating system | Windows, macOS, Android, AIX, FreeBSD, Linux, OpenBSD, Solaris, Windows Mobile |
| Platform | Java 1.4 or later (for JNA 3.5.2 or earlier), Java 1.6 for JNA 4.0.0 and later |
| Size | 1.83 MB (archived) |
| Type | Software Library |
| License | LGPL version 2.1 or later and (from version 4.0 onward) the Apache Software License, version 2.0 |
| Website | github |
Script error: No such module "Check for conflicting parameters".
Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). 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 (libffi) 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.[2]
| Native Type | Size | Java Type | Common Windows Types |
|---|---|---|---|
| Page Template:Mono/styles.css has no content.char | 8-bit integer | Page Template:Mono/styles.css has no content.byte | Page Template:Mono/styles.css has no content.BYTE, TCHAR |
| Page Template:Mono/styles.css has no content.short | 16-bit integer | Page Template:Mono/styles.css has no content.short | Page Template:Mono/styles.css has no content.WORD |
| Page Template:Mono/styles.css has no content.wchar_t | 16/32-bit character | Page Template:Mono/styles.css has no content.char | Page Template:Mono/styles.css has no content.TCHAR |
| Page Template:Mono/styles.css has no content.int | 32-bit integer | Page Template:Mono/styles.css has no content.int | Page Template:Mono/styles.css has no content.DWORD |
| Page Template:Mono/styles.css has no content.int | boolean value | Page Template:Mono/styles.css has no content.boolean | Page Template:Mono/styles.css has no content.BOOL |
| Page Template:Mono/styles.css has no content.long | 32/64-bit integer | Page Template:Mono/styles.css has no content.com.sun.jna.NativeLong | Page Template:Mono/styles.css has no content.LONG |
| long long | 64-bit integer | Page Template:Mono/styles.css has no content.long | Page Template:Mono/styles.css has no content.__int64 |
| Page Template:Mono/styles.css has no content.float | 32-bit FP | Page Template:Mono/styles.css has no content.float | |
| Page Template:Mono/styles.css has no content.double | 64-bit FP | Page Template:Mono/styles.css has no content.double | |
| Page Template:Mono/styles.css has no content.char* | C string | Page Template:Mono/styles.css has no content.String | Page Template:Mono/styles.css has no content.LPCSTR |
| Page Template:Mono/styles.css has no content.void* | Page Template:Mono/styles.css has no content.pointer | Page Template:Mono/styles.css has no content.com.sun.jna.Pointer | Page Template:Mono/styles.css has no content.LPVOID, HANDLE, LPXXX |
Note: The meaning of Page Template:Mono/styles.css has no content.TCHAR changes between Page Template:Mono/styles.css has no content.char and Page Template:Mono/styles.css has no content.wchar_t according to some preprocessor definitions. Page Template:Mono/styles.css has no content.LPCTSTR 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 Page Template:Mono/styles.css has no content.printf function.
Note: The following code is portable and works the same on Windows and POSIX (Linux / Unix / macOS) 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 {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary(
(Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i = 0; i < args.length; i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
The following program loads the C POSIX library and uses it to call the standard Page Template:Mono/styles.css has no content.mkdir 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 {
public interface Posix extends Library {
public int chmod(String filename, int mode);
public int chown(String filename, int user, int group);
public int rename(String oldpath, String newpath);
public int kill(int pid, int signal);
public int link(String oldpath, String newpath);
public int mkdir(String path, int mode);
public int rmdir(String path);
}
public static void main(String[] args) {
// It is possible to load msvcrt for its partial POSIX support on Windows...
Posix posix = (Posix) Native.loadLibrary("c", Posix.class);
// but it will still fail on Windows due to /tmp being missing.
posix.mkdir("/tmp/newdir", 0777);
posix.rename("/tmp/newdir","/tmp/renamedir");
}
}
The program below loads the Page Template:Mono/styles.css has no content.Kernel32.dll and uses it to call the Page Template:Mono/styles.css has no content.Beep and Page Template:Mono/styles.css has no content.Sleep 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 {
public interface Kernel32 extends Library {
// FREQUENCY is expressed in hertz and ranges from 37 to 32767
// DURATION is expressed in milliseconds
public boolean Beep(int FREQUENCY, int DURATION);
public void Sleep(int DURATION);
}
public static void main(String[] args) {
Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
lib.Beep(698, 500);
lib.Sleep(500);
lib.Beep(698, 500);
}
}
See also
Lua error in mw.title.lua at line 404: bad argument #2 to 'title.new' (unrecognized namespace name 'Portal').
References
Page Template:Reflist/styles.css has no content.
- ^ Page Module:Citation/CS1/styles.css has no content."Release 5.17.0". GitHub. 2025-03-16.
- ^ Page Module:Citation/CS1/styles.css has no content."Default Type Mappings". jna.dev.java.net. Retrieved 2011-08-02.
External links
- Java Native Access Web Page
- Java Native Access - Download page
- Java Native Access - User Mailing List
- Page Module:Citation/CS1/styles.css has no content.Friesen, Jeff (5 February 2008). "Open source Java projects: Java Native Access". Open Source Java Tutorials. JavaWorld. Retrieved 2020-07-27.
- Page Module:Citation/CS1/styles.css has no content.Morris, Stephen B. (20 May 2009). "Protect Your Legacy Code Investment with JNA". today.java.net. Archived from the original on 2015-01-13.
- Page Module:Citation/CS1/styles.css has no content.Dasgupta, Sanjay (11 November 2009). "Simplify Native Code Access with JNA". today.java.net. Archived from the original on 2009-11-15.
- Page Module:Citation/CS1/styles.css has no content.Doubrovkine, Daniel (20 June 2011). "JNA is now a Githubber". code.dblock.org. Retrieved 2020-07-27.
- Page Module:Citation/CS1/styles.css has no content.Kiaer, Jesper (21 March 2010). "Calling the Lotus Domino C-API with JNA". Nevermind.dk. Retrieved 2020-07-27.