Java Native Access

Java Native Access
Basisdaten

Hauptentwickler Todd Fast, Timothy Wall, Liang Chen
Aktuelle Version 5.17.0
(16. März 2025)
Betriebssystem Windows, OS X, Android, AIX, FreeBSD, Linux, OpenBSD, Solaris, Windows Mobile
Programmier­sprache Java
Kategorie Software-Bibliothek
Lizenz LGPL Version 2.1 oder höher und (ab V4.0) die Apache Software License V2.0
github.com/java-native-access/jna

Java Native Access (JNA) ist eine Java-Programmbibliothek für den Zugriff auf plattformspezifische („native“) dynamische Programmbibliotheken (DLLs in Windows oder „shared libraries“ auf anderen Systemen). Hierbei braucht im Unterschied zu Java Native Interface (JNI) kein plattformspezifischer Code geschrieben zu werden.

JNA ist in der Funktion mit den Platform Invocation Services (P/Invoke) des .Net-Frameworks unter Windows vergleichbar. Es unterstützt eine automatische Umwandlung zwischen einigen C- und Java-Datentypen. Die minimal erforderliche Java-Version ist 1.4.

Lizenz

LGPL Version 2.1 oder höher und (ab V4.0) die Apache Software License V2.0.[1]

Mapping der Datentypen

Die folgende Tabelle zeigt, wie das Mapping zwischen Java und dem nativen Code mit JNA erfolgt.[2]

Nativer Typ Größe Java Typ 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

Anwendungen

Die folgenden Softwareprojekte verwenden JNA:

Beispiel

Das folgende Beispiel lädt die Standard C Library, um die printf-Funktion aufzurufen. Dieses Beispiel funktioniert auf Microsoft Windows und 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]);
    }
  }
}

Einzelnachweise

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.