Java Native Access

Java Native Access

Infobox Software
name = Java Native Access



caption =
developer =
latest release version = 3.0.5
latest release date = release date|2008|08|14
latest preview version =
latest preview date =
operating system = Cross-platform
size = 606.9 KB (archived)
programming language = C and Java
genre = Software Library
license = LGPL version 2.1 or later.
website = https://jna.dev.java.net

Java Native Access provides Java programs easy access to native shared libraries (DLLs on Windows) without using Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. No boilerplate or generated glue code is required. While some attention is paid to performance, correctness and ease of use take priority. The JNA library uses a small native library stub to dynamically invoke native code. 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 overhead of configuring and building JNI code.

Adoption

Java Native Access is known to be used in:
*JRuby use JNA for POSIX functionality [ [http://headius.blogspot.com/2007/09/java-native-access-jruby-true-posix.html Java Native Access + JRuby = True POSIX] ]
*Freedom for Media in Java (FMJ) [ [http://fmj-sf.net/ Freedom for Media in Java] ]
*IntelliJ IDEA IDE by the company JetBrains
*SVNKit pure Java Subversion client library.
*Videolan JVLC Java Multimedia Library.

Mapping types

The following table shows the mapping of types between Java and native code and supported by the JNA library.

Example

The following program loads the the stdio.h "standard input/output header" from C standard library and uses it to call the "printf" function. Note that the code is portable and works the same on Windows and Linux / Unix / Mac OS X platforms.

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 "); for (int i = 0; i < args.length; i++) { CLibrary.INSTANCE.printf("Argument %d: %s ", i, args [i] ); }

The following program loads the the C POSIX library and uses it to call the standard "mkdir" function. Note that the code is portable and works the same on POSIX standards platforms.

import com.sun.jna.Library;import com.sun.jna.Native;

/** Simple example of native C POSIX library declaration and usage. */public class exampleOfPOSIX { 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) { POSIX posix = (POSIX) Native.loadLibrary("c", POSIX.class); posix.mkdir("/tmp/newdir", 777); posix.rename("/tmp/newdir","/tmp/renamedir");

The program below loads the Kernel32.dll and uses it to call the "Beep" and "Sleep" functions.
Note: The following code works only on Windows platforms.import com.sun.jna.Library;import com.sun.jna.Native;

/** Simple example of Windows native library declaration and usage. */public class BeepExample { 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);

References

ee also

*Java Native Interface
*P/Invoke

External links

* [https://jna.dev.java.net Java Native Access Web Page]
* [http://www.javaworld.com/javaworld/jw-02-2008/jw-02-opensourcejava-jna.html Java Native Access:An easier way to access native code] By Jeff Friesen, JavaWorld.com, 02/05/08


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Java Native Access — (JNA) ist eine Java Programmbibliothek für den Zugriff auf plattformspezifische ( native ) dynamische Programmbibliotheken (DLL in Windows). Hierbei muss im Unterschied zu JNI kein plattform spezifischer Code geschrieben werden. JNA ist in der… …   Deutsch Wikipedia

  • Java Native Interface — (JNI) ist eine standardisierte Anwendungsprogrammierschnittstelle zum Aufruf von plattformspezifischen Funktionen bzw. Methoden aus der Programmiersprache Java heraus. Ein Java Programm, das JNI Aufrufe verwendet, ist nicht mehr… …   Deutsch Wikipedia

  • Java Native Interface — (JNI)  стандартный механизм для запуска кода, под управлением виртуальной машины Java (JVM), который написан на языках С/С++ или Ассемблера, и скомпонован в виде динамических библиотек, позволяет не использовать статическое связывание. Это… …   Википедия

  • Java Native Interface — JNI redirects here. For the city in the province of Buenos Aires, see Junín, Buenos Aires. The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and to be called[1] by… …   Wikipedia

  • Java Native Interface — Le JNI (Java Native Interface) est un framework qui permet au code Java s exécutant à l intérieur de la JVM d appeler et d être appelé[1] par des applications natives (c est à dire des programmes spécifiques au matériel et au système d… …   Wikipédia en Français

  • Java AWT Native Interface — is an interface for the Java programming language that enables rendering libraries compiled to native code to draw directly to a Java Abstract Window Toolkit (AWT) Javadoc:SE|java/awt|Canvas object drawing surface.The Java Native Interface (JNI)… …   Wikipedia

  • Java Virtual Machine Tools Interface — (JVMTI, or more properly, JVM TI) was introduced in J2SE 5.0 ( Tiger ). This interface allows a program to inspect the state and to control the execution of applications running in the Java Virtual Machine (JVM). JVMTI is designed to provide an… …   Wikipedia

  • Java performance — Programs written in Java have had a reputation for being slower and requiring more memory than those written in natively compiled languages such as C or C++ (see e.g. [cite web url=http://www.jelovic.com/articles/why java is slow.htm title=Why… …   Wikipedia

  • Java OpenGL — Infobox Software name = JOGL (JSR 231) caption = A demo screenshot illustrating real time refraction using JOGL on Mac OS X developer = Sun Microsystems Game Technology Group latest release version = 1.1.1 latest release date = May 22, 2008… …   Wikipedia

  • Comparison of Java and C++ — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”