Java Native Access

From Wikipedia, the free encyclopedia

Java Native Access
Latest release 3.0.3 / May 22, 2008
Written in C and Java
OS Cross-platform
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 is comparable to .NET Framework P/Invoke. Contrary to JNI, no boilerplate C glue code have to be written to be able to access the native library.

Contents

[edit] Adoption

Java Native Access is known to be used in:

[edit] Example

The following program load the the C standard library and use it to call the standard 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\n");
    for (int i=0;i < args.length;i++) {
      CLibrary.INSTANCE.printf("Argument %d: %s\n", i, 
         args[i]);
    }
  }
}

[edit] References

[edit] See also

[edit] External links