Java Native Interface

from Wikipedia, the free encyclopedia

Java Native Interface ( JNI ) is a standardized application programming interface (API) that enables platform-specific functions or methods to be called from the Java programming language .

In contrast to normal Java programs, a Java program that uses JNI calls is only platform-independent if the native program library is available on all platforms.

Intended use

JNI makes it possible to write native methods for situations in which it is not possible to exclusively use Java as the programming language. This is the case if, for example, the standard Java class library does not support certain platform-dependent features or other program libraries. Furthermore, it is possible via JNI to make another application programmed in another programming language accessible for Java. Many classes of the Java standard library are based on JNI, for example to enable file input and output or sound playback. By integrating Java performance and platform-dependent implementations into the standard library, the Java programmer and user can use these features in a secure and platform-independent manner.

JNI is sometimes referred to as an escape hatch for Java developers because it gives them access to functionality that would otherwise not be possible through the standard Java API. It represents the interface to other programming languages ​​such as C or C ++.

With JNI, it is possible functions of a Windows - DLL or a shared library under Linux or Mac OS X , in C or C ++ are programmed to call from Java. Conversely, “native” programs can call Java methods via JNI or execute a JVM .

Java calls native method

To call a native method from Java, it must first be nativedeclared as " ". In addition, the object itself must load the native program library by calling " " before the native method itself can be called. System.loadLibrary("<Name der Bibliothek>")

The Java program is compiled and then javaha header file is created with the software tool , which specifies the function declaration. The native program library can then be created with this header file.

One of the most popular applications of JNI is the Standard Widget Toolkit (SWT) program library , which enables elements of the graphical user interface of the operating system to be used via JNI .

Native program calls Java

Java code can be called from a native program. This is necessary, for example, to enable a close connection between Java and the native program.

See also

literature

Web links