1. Introduction
At times, it is necessary to use native (non-Java) codes (e.g., C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI).
JNI is difficult, as it involves two languages and runtimes.
I shall assume that you are familiar with:
- Java.
- C/C++ and the GCC Compiler (Read "GCC and Make").
- (For Windows) Cygwin or MinGW (Read "How to Setup Cygwin and MinGW").
2. Getting Started
2.1 JNI with C
Step 1: Write a Java Class HelloJNI.java that uses C Codes
|
|
The static initializer invokes System.loadLibrary()
to load the native library "hello
" (which contains a native method called sayHello()
) during the class loading. It will be mapped to "hello.dll
" in Windows; or "libhello.so
" in Unixes/Mac OS X. This library shall be included in Java's library path (kept in Java system variable java.library.path
). You could include the library into Java's library path via VM argument -Djava.library.path=/path/to/lib
. The program will throw a UnsatisfiedLinkError
if the library cannot be found in runtime.
Next, we declare the method sayHello()
as a native instance method, via keyword native
which denotes that this method is implemented in another language. A native method does not contain a body. The sayHello()
shall be found in the native library loaded.
The main()
method allocates an instance of HelloJNI
and invoke the native method sayHello()
.
Step 2: Compile the Java Program HelloJNI.java & Generate the C/C++ Header File HelloJNI.h
Starting from JDK 8, you should use "javac -h
" to compile the Java program AND generate C/C++ header file called HelloJNI.h
as follows:
> javac -h . HelloJNI.java
The "-h dir
" option generates C/C++ header and places it in the directory specified (in the above example, '.'
for the current directory).
Before JDK 8, you need to compile the Java program using javac
and generate C/C++ header using a dedicated javah
utility, as follows. The javah
utility is no longer available in JDK 10.
> javac HelloJNI.java
> javah HelloJNI
Inspect the header file HelloJNI.h
:
|
|
The header declares a C function Java_HelloJNI_sayHello
as follows:
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *, jobject);
The naming convention for the C function is Java_{package_and_classname}_{function_name}(JNI_arguments)
. The dot in package name is replaced by underscore.
The arguments are:
JNIEnv*
: reference to JNI environment, which lets you access all the JNI functions.jobject
: reference to "this
" Java object.
We are not using these arguments in this hello-world example, but will be using them later. Ignore the macros JNIEXPORT
and JNICALL
for the time being.
The extern "C"
is recognized by C++ compiler only. It notifies the C++ compiler that these functions are to be compiled using C's function naming protocol instead of C++ naming protocol. C and C++ have different function naming protocols as C++ support function overloading and uses a name mangling scheme to differentiate the overloaded functions. Read "Name Mangling".
Step 3: Implementing the C Program HelloJNI.c
|
|
Save the C program as "HelloJNI.c
".
The JNI header "jni.h
" provided by JDK is available under the "<JAVA_HOME>\include
" and "<JAVA_HOME>\include\win32
" (for Windows) or "<JAVA_HOME>\include\linux
" (for Ubuntu) [Check Mac OS X] directories, where <JAVA_HOME>
is your JDK installed directory (e.g., "c:\program files\java\jdk10.0.x
" for Windows).
The C function simply prints the message "Hello world!" to the console.
Step 4: Compile the C program HelloJNI.c
Finding the right compiler for your operating platform (Windows, Mac OS X, Ubuntu), for your JDK (32-bit, 64-bit), and figuring out the correct compiler options is the hardest part to get the JNI working!!!
(Windows) 64-bit JDK
We are going to use Cygwin. You need to take note of the followings for Windows:
- Windows/Intel uses these instruction sets: x86 is a 32-bit instruction set; i868 is an enhanced version of x86 (also 32-bit); x86_64 (or amd64) is a 64-bit instruction set.
- A 32-bit compiler can run on 32-bit or 64-bit (backward compatible) Windows, but 64-bit compiler can only run on 64-bit Windows.
- A 64-bit compiler could produce target of 32-bit or 64-bit.
- If you use Cygwin's GCC, the target could be native Windows or Cygwin. If the target is native Windows, the code can be distributed and run under Windows. However, if the target is Cygwin, to distribute, you need to distribute Cygwin runtime environment (
cygwin1.dll
). This is because Cygwin is a Unix emulator under Windows. - The above explains for the many versions of GCC under Cygwin.
For 64-bit JDK, you need to find a compiler that produces target of 64-bit native Windows. This is provided by MinGW-W64. You can install MinGW-W64 under Cygwin, by selecting packages "mingw64-x86_64-gcc-core
" (C compiler) and "mingw64-x86_64-gcc-g++
" (C++ compiler). The executables are "x86_64-w64-mingw32-gcc
" (C Compiler) and "x86_64-w64-mingw32-g++
" (C++ Compiler), respectively.
First, set the environment variable JAVA_HOME
to point the JDK installed directory (e.g., "c:\program files\java\jdk10.0.x
"). Follow the steps HERE.
Next, use the following commands to compile HelloJNI.c
into hello.dll
. In Windows, we reference the environment variable JAVA_HOME
as %JAVA_HOME%
in the command.
> x86_64-w64-mingw32-gcc -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
The compiler options used are:
-IheaderDir
: for specifying the header directory. In this case "jni.h
" (in "%JAVA_HOME%\include
") and "jni_md.h
" (in "%JAVA_HOME%\include\win32"
), whereJAVA_HOME
is an environment variable set to the JDK installed directory.-shared
: to generate share library.-o outputFilename
: for setting the output filename "hello.dll
".
You can also compile and link in two steps:
// Compile-only "HelloJNI.c" with -c flag. Output is "HElloJNI.o"
> x86_64-w64-mingw32-gcc -c -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" HelloJNI.c
// Link "HelloJNO.o" into shared library "hello.dll"
> x86_64-w64-mingw32-gcc -shared -o hello.dll HelloJNI.o
You need check the resultant file type via the "file
" utility, which indicates "Hello.dll
" is a 64-bit (x86_64) native Windows DLL.
> file hello.dll
hello.dll: PE32+ executable (DLL) (console) x86-64, for MS Windows
Try nm
, which lists all the symbols in the shared library and look for the sayHello()
function. Check for the function name Java_HelloJNI_sayHello
with type "T"
(defined).
> nm hello.dll | grep say
00000000624014a0 T Java_HelloJNI_sayHello
(Windows) 32-bit JDK [Obsolete?]
For 32-bit JDK, you need to find a 32/64-bit compiler that produces target of 32-bit native Windows. This is provided by MinGW-W64 (and the older MinGW). You can install MinGW-W64 under Cygwin, by selecting packages "mingw64-i686-gcc-core
" (C compiler) and "mingw64-i686-gcc-g++
" (C++ compiler). The executables are "i886-w64-mingw32-gcc
" (C Compiler) and "i686-w64-mingw32-g++
" (C++ Compiler), respectively.
First, set the environment variable JAVA_HOME
to point the JDK installed directory (e.g., "c:\program files\java\jdk9.0.x
"). Follow the steps HERE.
Next, use the following command to compile HelloJNI.c
into hello.dll
:
> i886-w64-mingw32-gcc -Wl,--add-stdcall-alias -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
The compiler options used are:
-Wl
: The-Wl
to pass linker option--add-stdcall-alias
to preventUnsatisfiedLinkError
(symbols with a stdcall suffix (@nn
) will be exported as-is and also with the suffix stripped). (Some people suggested to use-Wl,--kill-at
.)-I
: for specifying the header files directories. In this case "jni.h
" (in "%JAVA_HOME%\include
") and "jni_md.h
" (in "%JAVA_HOME%\include\win32"
), where%JAVA_HOME%
is an environment variable set to the JDK installed directory.-shared
: to generate share library.-o
: for setting the output filename "hello.dll
".-D
__int64
="long long"
: define the type (add this option in front if error "unknown type name '__int64'")
(Ubuntu) 64-bit JDK
- Set environment variable
JAVA_HOME
to point to the JDK installed directory (which shall contains theinclude
subdirectory to be used in the next step):$ export JAVA_HOME=/your/java/installed/dir$ echo $JAVA_HOME
- Compile the C program
HelloJNI.c
into share modulelibhello.so
usinggcc
, which is included in all Unixes:$ gcc -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libhello.so HelloJNI.c
- Run the Java Program:
$ java -Djava.library.path=. HelloJNI
(Mac OS X) 64-Bit JDK
- Set environment variable
JAVA_HOME
to point to the JDK installed directory (which shall contains theinclude
subdirectory to be used in the next step):$ export JAVA_HOME=/your/java/installed/dir // for my machine @ /Library/Java/JavaVirtualMachines/jdk1.8.0_xx.jdk/Contents/Home $ echo $JAVA_HOME
- Compile the C program
HelloJNI.c
into dynamic share modulelibhello.dylib
usinggcc
, which is included in all Unixes/Mac OS:$ gcc -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/darwin" -dynamiclib -o libhello.dylib HelloJNI.c
- Run the Java Program:
$ java -Djava.library.path=. HelloJNI
Step 4: Run the Java Program
> java HelloJNI
You may need to explicitly specify the Java library path of the "hello.dll
" (Windows), "libHello.so
" (Unixes), "libhello.dylib
" (Mac OS X) via VM option -Djava.library.path=/path/to/lib
, as below. In this example, the native library is kept in the current directory '.'
.
> java -Djava.library.path=. HelloJNI
2.2 JNI with C++
Instead of a C program, we can use a C++ program (called HelloJNI.cpp
) for the above example.
|
|
Compile the C++ programs into shared library as follows. See "JNI with C" section for explanation.
(Windows) 64-bit JDK
On Cygwin, you need to install mingw64-x86-gcc-g++
package.
> x86_64-w64-mingw32-g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.cpp
(Ubuntu) 64-bit JDK
$ g++ -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libhello.so HelloJNI.cpp
(Mac OS X) 64-bit JDK
[TODO]
Run the Java Program
> java HelloJNI
or
> java -Djava.library.path=. HelloJNI
Notes: If you encounter "java.lang.UnsatisfiedLinkError: hello.dll: Can't find dependent libraries
", you need to find a "DLL dependency walker" to track down the dependent libraries. Search for the libraries (under Cygwin) and include the libraries in the environment variable PATH
. In my case, the dependent library is "libstdc++-6.dll
" located at "cygwin64\usr\x86_64-w64-mingw32\sys-root\mingw\bin
".
2.3 JNI with C/C++ Mixture
Step 1: Write a Java Class that uses Native Codes - HelloJNICpp.java
|
|
Step 2: Compile the Java Program & Generate the C/C++ Header file HelloJNICpp.h
javac -h . HelloJNICpp
The resultant header file "HelloJNICpp.h
" declares the native function as:
JNIEXPORT void JNICALL Java_HelloJNICpp_sayHello(JNIEnv *, jobject);
Step 3: C/C++ Implementation - HelloJNICppImpl.h, HelloJNICppImpl.cpp, and HelloJNICpp.c
We shall implement the program in C++ (in "HelloJNICppImpl.h
" and "HelloJNICppImpl.cpp
"), but use a C program ("HelloJNICpp.c
") to interface with Java.
C++ Header - "HelloJNICppImpl.h
"
|
|
C++ Implementation - "HelloJNICppImpl.cpp
"
|
|
C Program interfacing with Java - "HelloJNICpp.c
"
|
|
Compile the C/C++ programs into shared library ("hello.dll
" for Windows).
(Windows) 64-bit JDK
On Cygwin, you need to install mingw64-x86-gcc-g++
package.
> x86_64-w64-mingw32-g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNICpp.c HelloJNICppImpl.cpp
(Ubuntu) 64-bit JDK
$ g++ -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libhello.so HelloJNICpp.c HelloJNICppImpl.cpp
Step 4: Run the Java Program
> java HelloJNICpp
or
> java -Djava.library.path=. HelloJNICpp
Notes: If you encounter "java.lang.UnsatisfiedLinkError: hello.dll: Can't find dependent libraries
", you need to find a DLL dependency walker to track down the dependent libraries. Search for the libraries (under Cygwin) and include the libraries in the PATH
. In my case, the dependent library is "libstdc++-6.dll
" located at "cygwin64\usr\x86_64-w64-mingw32\sys-root\mingw\bin
".
2.4 JNI in Package
For production, all Java classes shall be kept in proper packages, instead of the default no-name package.
Step 1: JNI Program - myjni\HelloJNI.java
|
|
This JNI class is kept in package "myjni
" - to be saved as "myjni\HelloJNI.java
".
Step 2: Compile the JNI program & Generate C/C++ Header
// change directory to package base directory
> javac -h include myjni\HelloJNI
The output of compilation is "myjni\HelloJNI.class
".
In this example, we decided to place the header file under a "include
" sub-directory. The generated output is "include\myjni_HelloJNI.h
".
The header file declares a native function:
JNIEXPORT void JNICALL Java_myjni_HelloJNI_sayHello(JNIEnv *, jobject);
Take note of the native function naming convention: Java_<fully-qualified-name>_methodName
, with dots replaced by underscores.
Step 3: C Implementation - HelloJNI.c
|
|
Compile the C program:
// for Windows 64-bit JDK
> x86_64-w64-mingw32-gcc -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o hello.dll HelloJNI.c
You can now run the JNI program:
> java -Djava.library.path=. myjni.HelloJNI
2.5 JNI in Module (JDK 9)
[TODO]
2.6 JNI in Eclipse [To Check]
Writing JNI under Eclipse is handy for development Android apps with NDK.
You need to install Eclipse and Eclipse CDT (C/C++ Development Tool) Plugin. Read "Eclipse for C/C++" on how to install CDT.
Step 1: Create a Java Project
Create a new Java project (says HelloJNI
), and the following Java class "HelloJNI.java
":
public class HelloJNI {
static {
System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes)
}
// Declare native method
private native void sayHello();
// Test Driver
public static void main(String[] args) {
new HelloJNI().sayHello(); // Allocate an instance and invoke the native method
}
}
Step 2: Convert the Java Project to C/C++ Makefile Project
Right-click on the "HelloJNI
" Java project ⇒ New ⇒ Other... ⇒ Convert to a C/C++ Project (Adds C/C++ Nature) ⇒ Next.
The "Convert to a C/C++ Project" dialog appears. In "Project type", select "Makefile Project" ⇒ In "Toolchains", select "MinGW GCC" ⇒ Finish.
Now, you can run this project as a Java as well as C/C++ project.
Step 3: Generate C/C++ Header File (Pre JDK-10)
Create a directory called "jni
" under the project to keep all the C/C++ codes, by right-click on the project ⇒ New ⇒ Folder ⇒ In "Folder name", enter "jni
".
Create a "makefile
" under the "jni
" directory, by right-click on the "jni
" folder ⇒ new ⇒ File ⇒ In "File name", enter "makefile
" ⇒ Enter the following codes. Take note that you need to use tab (instead of spaces) for the indent.
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
# $* matches the target filename without the extension
# Pre JDK-10. JDK 10 removes the javah utility, need to use javac -h instead [TO CHECK]
HelloJNI.h : HelloJNI.class
javah -classpath $(CLASS_PATH) $*
This makefile create a target "HelloJNI.h
", which has a dependency "HelloJNI.class
", and invokes the javah
utility on HelloJNI.class
(under -classpath
) to build the target header file.
Right-click on the makefile ⇒ Make Targets ⇒ Create ⇒ In "Target Name", enter "HelloJNI.h
".
Run the makefile for the target "HelloJNI.h
", by right-click on the makefile ⇒ Make Targets ⇒ Build ⇒ Select the target "HelloJNI.h
" ⇒ Build. The header file "HelloJNI.h
" shall be generated in the "jni
" directory. Refresh (F5) if necessary. The outputs are:
make HelloJNI.h
javah -classpath ../bin HelloJNI
Read "GCC and Make" for details about makefile.
Alternatively, you could also use the CMD shell to run the make file:
// change directory to the directory containing makefile
> make HelloJNI.h
You can even use the CMD shell to run the javah
(Pre JDK-10):
> javah -classpath ../bin HelloJNI
Step 4: C Implementation - HelloJNI.c
Create a C program called "HelloJNI.c
", by right-click on the "jni
" folder ⇒ New ⇒ Source file ⇒ In "Source file", enter "HelloJNI.c
". Enter the following codes:
#include <jni.h>
#include <stdio.h>
#include "HelloJNI.h"
JNIEXPORT void JNICALL Java_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
Modify the "makefile
" as follows to generate the shared library "hello.dll
". (Again, use tab to indent the lines.)
# Define a variable for classpath
CLASS_PATH = ../bin
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hello.dll
# $@ matches the target, $< matches the first dependency
hello.dll : HelloJNI.o gcc -Wl,--add-stdcall-alias -shared -o $@ $< # $@ matches the target, $< matches the first dependency HelloJNI.o : HelloJNI.c HelloJNI.h gcc -I"D:\bin\jdk1.7\include" -I"D:\bin\jdk1.7\include\win32" -c $< -o $@ # $* matches the target filename without the extension HelloJNI.h : HelloJNI.class javah -classpath $(CLASS_PATH) $* clean : rm HelloJNI.h HelloJNI.o hello.dll
Right-click on the "makefile
" ⇒ Make Targets ⇒ Create ⇒ In "Target Name", enter "all
". Repeat to create a target "clean
".
Run the makefile for the target "all
", by right-click on the makefile ⇒ Make Targets ⇒ Build ⇒ Select the target "all
" ⇒ Build. The outputs are:
make all
javah -classpath ../bin HelloJNI
gcc -I"D:\bin\jdk1.7\include" -I"D:\bin\jdk1.7\include\win32" -c HelloJNI.c -o HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o hello.dll HelloJNI.o
The shared library "hello.dll
" shall have been created in "jni
" directory.
Step 5: Run the Java JNI Program
You can run the Java JNI program HelloJNI
. However, you need to provide the library path to the "hello.dll
". This can be done via VM argument -Djava.library.path
. Right-click on the project ⇒ Run As ⇒ Run Configurations ⇒ Select "Java Application" ⇒ In "Main" tab, enter the main class "HelloJNI
" ⇒ In "Arguments", "VM Arguments", enter "-Djava.library.path=jni
" ⇒ Run.
You shall see the output "Hello World!" displayed on the console.
2.7 JNI in NetBeans
[TODO]
3. JNI Basics
JNI defines the following JNI types in the native system that correspond to Java types:
- Java Primitives:
jint
,jbyte
,jshort
,jlong
,jfloat
,jdouble
,jchar
,jboolean
for Java Primitive ofint
,byte
,short
,long
,float
,double
,char
andboolean
, respectively. - Java Reference Types:
jobject
forjava.lang.Object
. It also defines the following sub-types:jclass
forjava.lang.Class
.jstring
forjava.lang.String
.jthrowable
forjava.lang.Throwable
.jarray
for Java array. Java array is a reference type with eight primitive array and oneObject
array. Hence, there are eight array of primitivesjintArray
,jbyteArray
,jshortArray
,jlongArray
,jfloatArray
,jdoubleArray
,jcharArray
andjbooleanArray
; and one object arrayjobjectArray
.
The native functions receives argument in the above JNI types and returns a value in the JNI type (such as jstring
, jintArray
). However, native functions operate on their own native types (such as C-string, C's int[]
). Hence, there is a need to convert (or transform) between JNI types and the native types.
The native programs:
- Receive the arguments in JNI type (passed over by the Java program).
- For reference JNI type, convert or copy the arguments to local native types, e.g.,
jstring
to a C-string,jintArray
to C'sint[]
, and so on. Primitive JNI types such asjint
andjdouble
do not need conversion and can be operated directly. - Perform its operations, in local native type.
- Create the returned object in JNI type, and copy the result into the returned object.
- Return.
The most confusing and challenging task in JNI programming is the conversion (or transformation) between JNI reference types (such as jstring
, jobject
, jintArray
, jobjectArray
) and native types (C-string
, int[]
). The JNI Environment interface provides many functions to do the conversion.
JNI is a C interface, which is not object-oriented. It does not really pass the objects.
[C++ object-oriented interface?!]
4. Passing Arguments and Result between Java & Native Programs
4.1 Passing Primitives
Passing Java primitives is straight forward. A jxxx
type is defined in the native system, i.e,. jint
, jbyte
, jshort
, jlong
, jfloat
, jdouble
, jchar
and jboolean
for each of the Java's primitives int
, byte
, short
, long
, float
, double
, char
and boolean
, respectively.
Java JNI Program: TestJNIPrimitive.java
|
|
This JNI program loads a shared library myjni.dll
(Windows) or libmyjni.so
(Unixes). It declares a native
method average()
that receives two int
's and returns a
double
containing the average value of the two int
's. The main()
method invoke the average()
.
Compile the Java program into "TestJNIPrimitive.class
" and generate the C/C++ header file "TestJNIPrimitive.h
":
javac -h . TestJNIPrimitive.java
C Implementation - TestJNIPrimitive.c
The header file TestJNIPrimitive.h
contains a function declaration Java_TestJNIPrimitive_average()
which takes a JNIEnv*
(for accessing JNI environment interface), a jobject
(for referencing this object
), two jint
's (Java native method's two arguments) and returns a jdouble
(Java native method's return-type).
JNIEXPORT jdouble JNICALL Java_TestJNIPrimitive_average(JNIEnv *, jobject, jint, jint);
The JNI types jint
and jdouble
correspond to Java's type int
and double
, respectively.
The "jni.h
" and "win32\jni_mh.h
" (which is platform dependent) contains these typedef
statements for the eight JNI primitives and an additional jsize
.
It is interesting to note that jint
is mapped to C's long
(which is at least 32 bits), instead of of C's int
(which could be 16 bits). Hence, it is important to use jint
in the C program, instead of simply using int
. Cygwin does not support __int64
.
// In "win\jni_mh.h" - machine header which is machine dependent
typedef long jint;
typedef __int64 jlong;
typedef signed char jbyte;
// In "jni.h"
typedef unsigned char jboolean;
typedef unsigned short jchar;
typedef short jshort;
typedef float jfloat;
typedef double jdouble;
typedef jint jsize;
The implementation TestJNIPrimitive.c
is as follows:
|
|
Compile the C program into shared library (jni.dll
).
gcc -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o myjni.dll TestJNIPrimitive.c
Now, run the Java Program:
java -Djava.library.path=. TestJNIPrimitive
C++ Implementation - TestJNIPrimitive.cpp
|
|
Compile the C++ program:
g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o myjni.dll TestJNIPrimitive.cpp
4.2 Passing Strings
Java JNI Program: TestJNIString.java
|
|
This JNI program declares a native
method sayHello()
that receives a Java String
and returns a Java String
. The main()
method invokes the sayHello()
.
Compile the Java program and generate the C/C++ header file "TestJNIString.h
":
javac -h . TestJNIString.java
C Implementation - TestJNIString.c
The header file TestJNIString.h
contains this function declaration:
JNIEXPORT jstring JNICALL Java_TestJNIString_sayHello(JNIEnv *, jobject, jstring);
JNI defined a jstring
type to represent the Java String
. The last argument (of JNI type jstring
) is the Java String
passed into the C program. The return-type is also jstring
.
Passing strings is more complicated than passing primitives, as Java's String
is an object (reference type), while C-string is a NULL-terminated char
array. You need to convert between Java String
(represented as JNI jstring
) and C-string (char*
).
The JNI Environment (accessed via the argument JNIEnv*
) provides functions for the conversion:
- To get a C-string (
char*
) from JNI string (jstring
), invoke methodconst char* GetStringUTFChars(JNIEnv*, jstring, jboolean*)
. - To get a JNI string (
jstring
) from a C-string (char*
), invoke methodjstring NewStringUTF(JNIEnv*, char*)
.
The C implementation TestJNIString.c
is as follows.
- It receives the JNI string (
jstring
), convert into a C-string (char*
), viaGetStringUTFChars()
. - It then performs its intended operations - displays the string received and prompts user for another string to be returned.
- It converts the returned C-string (
char*
) to JNI string (jstring
), viaNewStringUTF()
, and return thejstring
.
|
|
Compile the C program into shared library.
gcc -I"<JAVA_HOME>\include" -I"<JAVA_HOME>\include\win32" -shared -o myjni.dll TestJNIString.c
Now, run the Java Program:
java -Djava.library.path=. TestJNIString
In C, the received string is: Hello from Java
Enter a String: test
In Java, the returned string is: test
JNI Native String Functions
JNI supports conversion for Unicode (16-bit characters) and UTF-8 (encoded in 1-3 bytes) strings. UTF-8 strings act like null-terminated C-strings (char
array), which should be used in C/C++ programs.
The JNI string (jstring
) functions are:
// UTF-8 String (encoded to 1-3 byte, backward compatible with 7-bit ASCII)
// Can be mapped to null-terminated char-array C-string
const char * GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy);
// Returns a pointer to an array of bytes representing the string in modified UTF-8 encoding.
void ReleaseStringUTFChars(JNIEnv *env, jstring string, const char *utf);
// Informs the VM that the native code no longer needs access to utf.
jstring NewStringUTF(JNIEnv *env, const char *bytes);
// Constructs a new java.lang.String object from an array of characters in modified UTF-8 encoding.
jsize GetStringUTFLength(JNIEnv *env, jstring string);
// Returns the length in bytes of the modified UTF-8 representation of a string. void GetStringUTFRegion(JNIEnv *env, jstring str, jsize start, jsize length, char *buf); // Translates len number of Unicode characters beginning at offset start into modified UTF-8 encoding // and place the result in the given buffer buf. // Unicode Strings (16-bit character) const jchar * GetStringChars(JNIEnv *env, jstring string, jboolean *isCopy); // Returns a pointer to the array of Unicode characters void ReleaseStringChars(JNIEnv *env, jstring string, const jchar *chars); // Informs the VM that the native code no longer needs access to chars. jstring NewString(JNIEnv *env, const jchar *unicodeChars, jsize length); // Constructs a new java.lang.String object from an array of Unicode characters. jsize GetStringLength(JNIEnv *env, jstring string); // Returns the length (the count of Unicode characters) of a Java string. void GetStringRegion(JNIEnv *env, jstring str, jsize start, jsize length, jchar *buf); // Copies len number of Unicode characters beginning at offset start to the given buffer buf
UTF-8 strings or C-strings
The GetStringUTFChars()
function can be used to create a new C-string (char*
) from the given Java's jstring
. The function returns NULL
if the memory cannot be allocated. It is always a good practice to check against NULL
.
The 3rd parameter isCopy
(of jboolean*
), which is an "in-out" parameter, will be set to JNI_TRUE
if the returned string is a copy of the original java.lang.String
instance. It will be set to JNI_FALSE
if the returned string is a direct pointer to the original String
instance - in this case, the native code shall not modify the contents of the returned string. The JNI runtime will try to return a direct pointer, if possible; otherwise, it returns a copy. Nonetheless, we seldom interested in modifying the underlying string, and often pass a NULL
pointer.
Always invoke ReleaseStringUTFChars()
whenever you do not need the returned string of GetStringUTFChars()
to release the memory and the reference so that it can be garbage-collected.
The NewStringUTF()
function create a new JNI string (jstring
), with the given C-string.
JDK 1.2 introduces the GetStringUTFRegion()
, which copies the jstring
(or a portion from start
of length
) into the "pre-allocated" C's char
array. They can be used in place of GetStringUTFChars()
. The isCopy
is not needed as the C's array is pre-allocated.
JDK 1.2 also introduces the Get/ReleaseStringCritical()
functions. Similar to GetStringUTFChars()
, it returns a direct pointer if possible; otherwise, it returns a copy. The native method shall not block (for IO or others) between a pair a GetStringCritical()
and ReleaseStringCritical()
call.
For detailed description, always refer to "Java Native Interface Specification" @ http://docs.oracle.com/javase/7/docs/technotes/guides/jni/index.html.
Unicode String
Instead of char*
, it uses a jchar*
to store the Unicode characters.
C++ Implementation - TestJNIString.cpp
|
|
Use g++
to compile the C++ program:
g++ -I"<JAVA_HOME>\include" -I"<JAVA_HOME>\include\win32" -shared -o myjni.dll TestJNIString.cpp
Take note that C++ native string functions have different syntax from C. In C++, we could us "env->
", instead of "(env*)->
". Furthermore, there is no need for the JNIEnv*
argument in the C++ functions.
Also take note that C++ support a string
class (under the header <string>
which is more user-friendly, as well as the legacy C-string (char array).
[TODO] Is C++ string
class supported directly?
4.3 Passing Array of Primitives
JNI Program - TestJNIPrimitiveArray.java
|
|
C Implementation - TestJNIPrimitiveArray.c
The header "TestJNIPrimitiveArray.h
" contains the following function declaration:
JNIEXPORT jdoubleArray JNICALL Java_TestJNIPrimitiveArray_average (JNIEnv *, jobject, jintArray);
In Java, array is a reference type, similar to a class. There are 9 types of Java arrays, one each of the eight primitives and an array of java.lang.Object
. JNI defines a type for each of the eight Java primitive arrays, i.e, jintArray
, jbyteArray
, jshortArray
, jlongArray
, jfloatArray
, jdoubleArray
, jcharArray
, jbooleanArray
for Java's primitive array of int
, byte
, short
, long
, float
, double
, char
and boolean
, respectively. It also define a jobjectArray
for Java's array of Object
(to be discussed later).
Again, you need to convert between JNI array and native array, e.g., between jintArray
and C's jint[]
, or jdoubleArray
and C's jdouble[]
. The JNI Environment interface provides a set of functions for the conversion:
- To get a C native
jint[]
from a JNIjintArray
, invokejint* GetIntArrayElements(JNIEnv *env, jintArray a, jboolean *iscopy)
. - To get a JNI
jintArray
from C nativejint[]
, first, invokejintArray NewIntArray(JNIEnv *env, jsize len)
to allocate, then usevoid SetIntArrayRegion(JNIEnv *env, jintArray a, jsize start, jsize len, const jint *buf)
to copy from thejint[]
tojintArray
.
There are 8 sets of the above functions, one for each of the eight Java primitives.
The native program is required to:
- Receive the incoming JNI array (e.g.,
jintArray
), convert to C's native array (e.g.,jint[]
). - Perform its intended operations.
- Convert the return C's native array (e.g.,
jdouble[]
) to JNI array (e.g.,jdoubleArray
), and return the JNI array.
The C implementation "TestJNIPrimitiveArray.c
" is:
|
|
JNI Primitive Array Functions
The JNI primitive array (jintArray
, jbyteArray
, jshortArray
, jlongArray
, jfloatArray
, jdoubleArray
, jcharArray
andjbooleanArray
) functions are:
// ArrayType: jintArray, jbyteArray, jshortArray, jlongArray, jfloatArray, jdoubleArray, jcharArray, jbooleanArray
// PrimitiveType: int, byte, short, long, float, double, char, boolean
// NativeType: jint, jbyte, jshort, jlong, jfloat, jdouble, jchar, jboolean
NativeType * Get<PrimitiveType>ArrayElements(JNIEnv *env, ArrayType array, jboolean *isCopy);
void Release<PrimitiveType>ArrayElements(JNIEnv *env, ArrayType array, NativeType *elems, jint mode);
void Get<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize start, jsize length, NativeType *buffer);
void Set<PrimitiveType>ArrayRegion(JNIEnv *env, ArrayType array, jsize start, jsize length, const NativeType *buffer);
ArrayType New<PrimitiveType>Array(JNIEnv *env, jsize length);
void * GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy);
void ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode);
The GET|Release<PrimitiveType>ArrayElements()
can be used to create a new C's native array jxxx[]
from the given Java jxxxArray
. GET|Set<PrimitiveType>ArrayRegion()
can be used to copy a jxxxArray
(or a portion from start
of length
) to and from a pre-allocated C native array jxxx[]
.
The New<PrimitiveType>Array()
can be used to allocate a new jxxxArray
of a given size. You can then use the Set<PrimitiveType>ArrayRegion()
function to fill its contents from a native array jxxx[]
.
The Get|ReleasePrimitiveArrayCritical()
functions does not allow blocking calls in between the get and release.
5. Accessing Object's Variables and Calling Back Methods
5.1 Accessing Object's Instance Variables
JNI Program - TestJNIInstanceVariable.java
|
|
The class contains two private
instance variables: a primitive int
called number
and a String
called message
. It also declares a native method, which could modify the contents of the instance variables.
C Implementation - TestJNIInstanceVariable.c
|
|
To access the instance variable of an object:
- Get a reference to this object's class via
GetObjectClass()
. - Get the Field ID of the instance variable to be accessed via
GetFieldID()
from the class reference. You need to provide the variable name and its field descriptor (or signature). For a Java class, the field descriptor is in the form of "L<fully-qualified-name>;
", with dot replaced by forward slash (/
), e.g.,, the class descriptor forString
is "Ljava/lang/String;
". For primitives, use"I"
forint
,"B"
forbyte
,"S"
forshort
,"J"
forlong
,"F"
forfloat
,"D"
fordouble
,"C"
forchar
, and"Z"
forboolean
. For arrays, include a prefix"["
, e.g., "[Ljava/lang/Object;
" for an array ofObject
;"[I"
for an array ofint
. - Based on the Field ID, retrieve the instance variable via
GetObjectField()
orGet<primitive-type>Field()
function. - To update the instance variable, use the
SetObjectField()
orSet<primitive-type>Field()
function, providing the Field ID.
The JNI functions for accessing instance variable are:
jclass GetObjectClass(JNIEnv *env, jobject obj);
// Returns the class of an object.
jfieldID GetFieldID(JNIEnv *env, jclass cls, const char *name, const char *sig);
// Returns the field ID for an instance variable of a class.
NativeType Get<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID);
void Set<type>Field(JNIEnv *env, jobject obj, jfieldID fieldID, NativeType value);
// Get/Set the value of an instance variable of an object
// <type> includes each of the eight primitive types plus Object.
5.2 Accessing Class' Static Variables
Accessing static variables is similar to accessing instance variable, except that you use functions such as GetStaticFieldID()
, Get|SetStaticObjectField()
, Get|SetStatic<Primitive-type>Field()
.
JNI Program - TestJNIStaticVariable.java
|
|
C Implementation - TestJNIStaticVariable.c
|
|
The JNI functions for accessing static variable are:
jfieldID GetStaticFieldID(JNIEnv *env, jclass cls, const char *name, const char *sig);
// Returns the field ID for a static variable of a class.
NativeType GetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID);
void SetStatic<type>Field(JNIEnv *env, jclass clazz, jfieldID fieldID, NativeType value);
// Get/Set the value of a static variable of a class.
// <type> includes each of the eight primitive types plus Object.
5.3 Callback Instance Methods and Static Methods
You can callback an instance and static methods from the native code.
JNI Program - TestJNICallBackMethod.java
|
|
This class declares a native
method called nativeMethod()
, and invoke this nativeMethod()
. The nativeMethod()
, in turn, calls back the various instance and static methods defined in this class.
C Implementation - TestJNICallBackMethod.c
|
|
To call back an instance method from the native code:
- Get a reference to this object's class via
GetObjectClass()
. - From the class reference, get the Method ID via
GetMethodID()
. You need to provide the method name and the signature. The signature is in the form "(parameters)return-type
". You can list the method signature for a Java program viajavap
utility (Class File Disassembler) with-s
(print signature) and-p
(show private members):> javap --help > javap -s -p TestJNICallBackMethod ....... private void callback(); Signature: ()V private void callback(java.lang.String); Signature: (Ljava/lang/String;)V private double callbackAverage(int, int); Signature: (II)D private static java.lang.String callbackStatic(); Signature: ()Ljava/lang/String; .......
- Based on the Method ID, you could invoke
Call<Primitive-type>Method()
orCallVoidMethod()
orCallObjectMethod()
, where the return-type is<Primitive-type>
, void andObject
, respectively. Append the argument, if any, before the argument list. For non-void
return-type, the method returns a value.
To callback a static
method, use GetMethodID(),
CallStatic<Primitive-type>Method()
, CallStaticVoidMethod()
or CallStaticObjectMethod()
.
The JNI functions for calling back instance method and static method are:
jmethodID GetMethodID(JNIEnv *env, jclass cls, const char *name, const char *sig);
// Returns the method ID for an instance method of a class or interface.
NativeType Call<type>Method(JNIEnv *env, jobject obj, jmethodID methodID, ...);
NativeType Call<type>MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args);
NativeType Call<type>MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args);
// Invoke an instance method of the object.
// The <type> includes each of the eight primitive and Object.
jmethodID GetStaticMethodID(JNIEnv *env, jclass cls, const char *name, const char *sig);
// Returns the method ID for an instance method of a class or interface.
NativeType CallStatic<type>Method(JNIEnv *env, jclass clazz, jmethodID methodID, ...);
NativeType CallStatic<type>MethodA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args);
NativeType CallStatic<type>MethodV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args);
// Invoke an instance method of the object.
// The <type> includes each of the eight primitive and Object.
5.4 Callback Overridden Superclass' Instance Method
JNI provides a set of CallNonvirtual<Type>Method()
functions to invoke superclass' instance methods which has been overridden in this class (similar to a super.methodName()
call inside a Java subclass):
- Get the Method ID, via
GetMethodID()
. - Based on the Method ID, invoke one of the
CallNonvirtual<Type>Method()
, with the object, superclass, and arguments.
The JNI function for calling the overridden superclass' instance method are:
NativeType CallNonvirtual<type>Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...);
NativeType CallNonvirtual<type>MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args);
NativeType CallNonvirtual<type>MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args);
6. Creating Objects and Object Arrays
You can construct jobject
and jobjectArray
inside the native code, via NewObject()
and newObjectArray()
functions, and pass them back to the Java program.
6.1 Callback the Constructor to Create a New Java Object in the Native Code
Callback the constructor is similar to calling back method. First, get the Method ID of the constructor by passing "<init>
" as the method name and "V
" as the return-type. You can then use methods like NewObject()
to call the constructor to create a new java object.
JNI Program - TestJavaConstructor.java
|
|
This class declares a native
method getIntegerObject()
. The native code shall create and return an Integer object, based on the argument given.
C Implementation - TestJavaConstructor.c
|
|
The JNI functions for creating object (jobject
) are:
jclass FindClass(JNIEnv *env, const char *name);
jobject NewObject(JNIEnv *env, jclass cls, jmethodID methodID, ...);
jobject NewObjectA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args);
jobject NewObjectV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args);
// Constructs a new Java object. The method ID indicates which constructor method to invoke
jobject AllocObject(JNIEnv *env, jclass cls);
// Allocates a new Java object without invoking any of the constructors for the object.
6.2 Array of Objects
JNI Program - TestJNIObjectArray.java
|
|
For illustration, this class declares a native
method that takes an array of Integer
, compute their sum and average, and returns as an array of Double
. Take note the arrays of objects are pass into and out of the native method.
C Implementation - TestJNIObjectArray.c
|
|
Unlike primitive array which can be processed in bulk, for object array, you need to use the Get|SetObjectArrayElement()
to process each of the elements.
The JNI functions for creating and manipulating object array (jobjectArray
) are:
jobjectArray NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement);
// Constructs a new array holding objects in class elementClass.
// All elements are initially set to initialElement.
jobject GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index);
// Returns an element of an Object array.
void SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value);
// Sets an element of an Object array.
7. Local and Global References
Managing references is critical in writing efficient programs. For example, we often use FindClass()
, GetMethodID()
, GetFieldID()
to retrieve a jclass
, jmethodID
and jfieldID
inside native functions. Instead of performing repeated calls, the values should be obtained once and cached for subsequent usage, to eliminate the overheads.
The JNI divides object references (for jobject
) used by the native code into two categories: local and global references:
- A local reference is created within the native method, and freed once the method exits. It is valid for the duration of a native method. You can also use JNI function
DeleteLocalRef()
to invalidate a local reference explicitly, so that it is available for garbage collection intermediately. Objects are passed to native methods as local references. All Java objects (jobject
) returned by JNI functions are local references. - A global reference remains until it is explicitly freed by the programmer, via the
DeleteGlobalRef()
JNI function. You can create a new global reference from a local reference via JNI functionNewGlobalRef()
.
Example
|
|
The above JNI program declares two native methods. Both of them create and return a java.lang.Integer
object.
In the C implementation, we need to get a class reference for java.lang.Integer
, via FindClass()
. We then find the method ID for the constructor of Integer
, and invoke the constructor. However, we wish to cache both the class reference and method ID, to be used for repeated invocation.
The following C implementation does not work!
|
|
In the above program, we invoke FindClass()
to find the class reference for java.lang.Integer
, and saved it in a global static variable. Nonetheless, in the next invocation, this reference is no longer valid (and not NULL). This is because FindClass()
returns a local reference, which is invalidated once the method exits.
To overcome the problem, we need to create a global reference from the local reference returned by FindClass()
. We can then free the local reference. The revised code is as follows:
// Get a class reference for java.lang.Integer if missing
if (NULL == classInteger) {
printf("Find java.lang.Integer\n");
// FindClass returns a local reference
jclass classIntegerLocal = (*env)->FindClass(env, "java/lang/Integer");
// Create a global reference from the local reference
classInteger = (*env)->NewGlobalRef(env, classIntegerLocal);
// No longer need the local reference, free it!
(*env)->DeleteLocalRef(env, classIntegerLocal);
}
Take note that jmethodID
and jfieldID
are not jobject
, and cannot create global reference.
来源:oschina
链接:https://my.oschina.net/u/4262851/blog/3744618