Java Native Access

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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.

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. ==Mapping types== The following table shows an overview of types mapping between Java and native code and supported by the JNA library. Note: The meaning of TCHAR changes between char and wchar_t according to some preprocessor definitions. LPCTSTR follows. 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 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 { 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 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 { 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== JNAerator P/Invoke SWIG ==References== ==External links== Java Native Access Web Page Java Native Access - Download page Java Native Access - User Mailing List

Text derived from https://en.wikipedia.org/w/index.php?oldid=1362591718 under CC BY-SA 4.0; modifications have been made.

Provenance

snapshot enwiki-001362591718 · captured 1783203039.0 · version history

Cite this page as: encyclopegia.org, “Java Native Access”, snapshot enwiki-001362591718. https://encyclopegia.org/a/java-native-access