IBM User Guide for Java V7 on Windows > IBM SDK for Java > The Java Native Interface (JNI)
Overview of JNI
From the viewpoint of a JVM, there are two types of code: "Java™" and "native". The Java Native Interface (JNI) establishes a well-defined and platform-independent interface between the two.
Native code can be used together with Java in two distinct ways: as "native methods" in a running JVM and as the code that creates a JVM using the "Invocation API". This section describes the difference.
Native methods
Java native methods are declared in Java, implemented in another language (such as C or C++), and loaded by the JVM as necessary. To use native methods, you must:
- Declare the native method in your Java code.
When the javac compiler encounters a native method declaration in Java source code, it records the name and parameters for the method. Because the Java source code contains no implementation, the compiler marks the method as "native". The JVM can then resolve the method correctly when it is called.
- Implement the native method.
Native methods are implemented as external entry points in a loadable binary library. The contents of a native library are platform-specific. The JNI provides a way for the JVM to use any native methods in a platform-independent way.
The JVM performs calls to native methods. When the JVM is in a native method, JNI provides a way to "call back" to the JVM.
- Load the native method code for the VM to use.
As well as declaring the native method, you must find and load the native library that contains the method at run time.
Two Java interfaces load native libraries:
- java.lang.System.load()
- java.lang.System.loadLibrary()
Typically, a class that declares native methods loads the native library in its static initializer.
Invocation API
Creating a JVM involves native code. The aspect of the JNI used for this purpose is called the JNI Invocation API. To use the Invocation API, you bind to an implementation-specific shared library, either statically or dynamically, and call the JNI_* functions it exports.
The JNI specification and implementation
The JNI specification is vague on selected implementation details. It provides a reusable framework for simple and extensible C and C++ native interfaces. The JNI model is also the basis for the JVMTI specification.
The Oracle Corporation trademark specification and the Java Compatibility Kit (JCK) ensure compliance to the specification but not to the implementation. Native code must conform to the specification and not to the implementation. Code written against unspecified behavior is prone to portability and forward compatibility problems.
Parent: The Java Native Interface (JNI)
Error 404 - Not Found Error 404 - Not Found
The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.