Java Native Access

from Wikipedia, the free encyclopedia
Java Native Access
Basic data

Maintainer Todd Fast, Timothy Wall, Liang Chen
Current  version 5.5.0
(October 30, 2019)
operating system Windows, OS X, Android, AIX, FreeBSD, Linux, OpenBSD, Solaris, Windows Mobile
programming language Java
category Software library
License LGPL Version 2.1 or higher and (from V4.0) the Apache Software License V2.0
github.com/java-native-access/jna

Java Native Access ( JNA ) is a Java - program library for access to platform-specific ( "native") dynamic libraries ( DLLs in Windows or "shared libraries" on other systems). In contrast to the Java Native Interface (JNI), no platform-specific code needs to be written.

The function of JNA is comparable to the Platform Invocation Services (P / Invoke) of .NET under Windows. It supports automatic conversion between some C and Java data types . The minimum required Java version is 1.4.

License

LGPL Version 2.1 or higher and (from V4.0) the Apache Software License V2.0.

Mapping of the data types

The following table shows how the mapping between Java and the native code is done with JNA.

Native type size Java type Standard Windows Type
char 8-bit integer byte BYTE, TCHAR
short 16-bit short short WORD
wchar_t 16/32-bit character char WCHAR, TCHAR
int 32-bit integer int DWORD
int boolean value boolean BOOL
long 32/64-bit integer NativeLong LONG
long long, __int64 64-bit integer long
float 32-bit FP float
double 64-bit FP double
char * C string String LPCTSTR
void * pointer pointer LPVOID, HANDLE, LPXXX

Applications

The following software projects use JNA:

example

The following example loads the Standard C Library to call the printf function. This example works on Microsoft Windows and Linux / Unix / Mac OS X .

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

/** Einfaches Beispiel einer Deklaration und Nutzung einer Dynamischen Programmbibliothek bzw. "shared library". */
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]);
    }
  }
}

Web links

Individual evidence

  1. github.com
  2. Default Type Mappings. jna.dev.java.net, accessed August 2, 2011 .