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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class HelloJNI { // Save as HelloJNI.java static { System.loadLibrary("hello"); // Load native library hello.dll (Windows) or libhello.so (Unixes) // at runtime // This library contains a native method called sayHello() } // Declare an instance native method sayHello() which receives no parameter and returns void private native void sayHello(); // Test Driver public static void main(String[] args) { new HelloJNI().sayHello(); // Create an instance and invoke the native method } } |
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()
.
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
/* DO NOT EDIT THIS FILE - it is machine generated */ #include |
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".
1 2 3 4 5 6 7 8 9 10 |
// Save as "HelloJNI.c" #include |
Save the C program as "HelloJNI.c
".
The JNI header "jni.h
" provided by JDK is available under the "
" and "
" (for Windows) or "
" (for Ubuntu) [Check Mac OS X] directories, where
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.
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!!!
We are going to use Cygwin. You need to take note of the followings for Windows:
cygwin1.dll
). This is because Cygwin is a Unix emulator under Windows.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"
), where JAVA_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
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 prevent UnsatisfiedLinkError
(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'")JAVA_HOME
to point to the JDK installed directory (which shall contains the include
subdirectory to be used in the next step): $ export JAVA_HOME=/your/java/installed/dir $ echo $JAVA_HOME
HelloJNI.c
into share module libhello.so
using gcc
, which is included in all Unixes: $ gcc -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libhello.so HelloJNI.c
$ java -Djava.library.path=. HelloJNI
JAVA_HOME
to point to the JDK installed directory (which shall contains the include
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
HelloJNI.c
into dynamic share module libhello.dylib
using gcc
, which is included in all Unixes/Mac OS: $ gcc -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/darwin" -dynamiclib -o libhello.dylib HelloJNI.c
$ java -Djava.library.path=. HelloJNI
> 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
Instead of a C program, we can use a C++ program (called HelloJNI.cpp
) for the above example.
1 2 3 4 5 6 7 8 9 10 11 |
// Save as "HelloJNI.cpp" #include |
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]
> 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
".
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class HelloJNICpp { static { System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes) } // Native method declaration private native void sayHello(); // Test Driver public static void main(String[] args) { new HelloJNICpp().sayHello(); // Invoke native method } } |
javac -h . HelloJNICpp
The resultant header file "HelloJNICpp.h
" declares the native function as:
JNIEXPORT void JNICALL Java_HelloJNICpp_sayHello(JNIEnv *, jobject);
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
"
1 2 3 4 5 6 7 8 9 10 11 12 |
#ifndef _HELLO_JNI_CPP_IMPL_H #define _HELLO_JNI_CPP_IMPL_H #ifdef __cplusplus extern "C" { #endif void sayHello (); #ifdef __cplusplus } #endif #endif |
C++ Implementation - "HelloJNICppImpl.cpp
"
1 2 3 4 5 6 7 8 9 |
#include "HelloJNICppImpl.h" #include |
C Program interfacing with Java - "HelloJNICpp.c
"
1 2 3 4 5 6 7 8 |
#include |
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
> 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
".
For production, all Java classes shall be kept in proper packages, instead of the default no-name package.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package myjni; public class HelloJNI { static { System.loadLibrary("hello"); // hello.dll (Windows) or libhello.so (Unixes) } // A native method that receives nothing and returns void private native void sayHello(); public static void main(String[] args) { new myjni.HelloJNI().sayHello(); // invoke the native method } } |
This JNI class is kept in package "myjni
" - to be saved as "myjni\HelloJNI.java
".
// 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.
1 2 3 4 5 6 7 8 |
#include |
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
[TODO]
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.
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 } }
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.
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
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#include #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 "
". Repeat to create a target "all
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.
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.
[TODO]
JNI defines the following JNI types in the native system that correspond to Java types:
jint
, jbyte
, jshort
, jlong
, jfloat
, jdouble
, jchar
, jboolean
for Java Primitive of int
, byte
, short
, long
, float
, double
, char
and boolean
, respectively.jobject
for java.lang.Object
. It also defines the following sub-types:
jclass
for java.lang.Class
.jstring
for java.lang.String
.jthrowable
for java.lang.Throwable
.jarray
for Java array. Java array is a reference type with eight primitive array and one Object
array. Hence, there are eight array of primitives jintArray
, jbyteArray
, jshortArray
, jlongArray
, jfloatArray
, jdoubleArray
, jcharArray
and jbooleanArray
; and one object array jobjectArray
.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:
jstring
to a C-string, jintArray
to C's int[]
, and so on. Primitive JNI types such as jint
and jdouble
do not need conversion and can be operated directly.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?!]
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class TestJNIPrimitive { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Declare a native method average() that receives two ints and return a double containing the average private native double average(int n1, int n2); // Test Driver public static void main(String args[]) { System.out.println("In Java, the average is " + new TestJNIPrimitive().average(3, 2)); } } |
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
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include |
Compile the C++ program:
g++ -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -shared -o myjni.dll TestJNIPrimitive.cpp
1 2 3 4 5 6 7 8 9 10 11 12 |
public class TestJNIString { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Native method that receives a Java String and return a Java String private native String sayHello(String msg); public static void main(String args[]) { String result = new TestJNIString().sayHello("Hello from Java"); System.out.println("In Java, the returned string is: " + result); } } |
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
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:
char*
) from JNI string (jstring
), invoke method const char* GetStringUTFChars(JNIEnv*, jstring, jboolean*)
.jstring
) from a C-string (char*
), invoke method jstring NewStringUTF(JNIEnv*, char*)
.The C implementation TestJNIString.c
is as follows.
jstring
), convert into a C-string (char*
), via GetStringUTFChars()
.char*
) to JNI string (jstring
), via NewStringUTF()
, and return the jstring
. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include |
Compile the C program into shared library.
gcc -I"\include" -I" \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 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
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.
Instead of char*
, it uses a jchar*
to store the Unicode characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include |
Use g++
to compile the C++ program:
g++ -I"\include" -I" \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
which is more user-friendly, as well as the legacy C-string (char array).
[TODO] Is C++ string
class supported directly?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class TestJNIPrimitiveArray { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Declare a native method sumAndAverage() that receives an int[] and // return a double[2] array with [0] as sum and [1] as average private native double[] sumAndAverage(int[] numbers); // Test Driver public static void main(String args[]) { int[] numbers = {22, 33, 33}; double[] results = new TestJNIPrimitiveArray().sumAndAverage(numbers); System.out.println("In Java, the sum is " + results[0]); System.out.println("In Java, the average is " + results[1]); } } |
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:
jint[]
from a JNI jintArray
, invoke jint* GetIntArrayElements(JNIEnv *env, jintArray a, jboolean *iscopy)
.jintArray
from C native jint[]
, first, invoke jintArray NewIntArray(JNIEnv *env, jsize len)
to allocate, then use void SetIntArrayRegion(JNIEnv *env, jintArray a, jsize start, jsize len, const jint *buf)
to copy from the jint[]
to jintArray
.There are 8 sets of the above functions, one for each of the eight Java primitives.
The native program is required to:
jintArray
), convert to C's native array (e.g., jint[]
).jdouble[]
) to JNI array (e.g., jdoubleArray
), and return the JNI array.The C implementation "TestJNIPrimitiveArray.c
" is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include |
The JNI primitive array (jintArray
, jbyteArray
, jshortArray
, jlongArray
, jfloatArray
, jdoubleArray
, jcharArray
and jbooleanArray
) 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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class TestJNIInstanceVariable { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Instance variables private int number = 88; private String message = "Hello from Java"; // Declare a native method that modifies the instance variables private native void modifyInstanceVariable(); // Test Driver public static void main(String args[]) { TestJNIInstanceVariable test = new TestJNIInstanceVariable(); test.modifyInstanceVariable(); System.out.println("In Java, int is " + test.number); System.out.println("In Java, String is " + test.message); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include |
To access the instance variable of an object:
GetObjectClass()
.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;
", with dot replaced by forward slash (/
), e.g.,, the class descriptor for String
is "Ljava/lang/String;
". For primitives, use "I"
for int
, "B"
for byte
, "S"
for short
, "J"
for long
, "F"
for float
, "D"
for double
, "C"
for char
, and "Z"
for boolean
. For arrays, include a prefix "["
, e.g., "[Ljava/lang/Object;
" for an array of Object
; "[I"
for an array of int
.GetObjectField()
or Get<primitive-type>Field()
function.SetObjectField()
or Set<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.
Accessing static variables is similar to accessing instance variable, except that you use functions such as GetStaticFieldID()
, Get|SetStaticObjectField()
, Get|SetStatic<Primitive-type>Field()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class TestJNIStaticVariable { static { System.loadLibrary("myjni"); // nyjni.dll (Windows) or libmyjni.so (Unixes) } // Static variables private static double number = 55.66; // Declare a native method that modifies the static variable private native void modifyStaticVariable(); // Test Driver public static void main(String args[]) { TestJNIStaticVariable test = new TestJNIStaticVariable(); test.modifyStaticVariable(); System.out.println("In Java, the double is " + number); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include |
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.
You can callback an instance and static methods from the native code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public class TestJNICallBackMethod { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Declare a native method that calls back the Java methods below private native void nativeMethod(); // To be called back by the native code private void callback() { System.out.println("In Java"); } private void callback(String message) { System.out.println("In Java with " + message); } private double callbackAverage(int n1, int n2) { return ((double)n1 + n2) / 2.0; } // Static method to be called back private static String callbackStatic() { return "From static Java method"; } // Test Driver public static void main(String args[]) { new TestJNICallBackMethod().nativeMethod(); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include |
To call back an instance method from the native code:
GetObjectClass()
.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 via javap
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; .......
CallMethod()
or CallVoidMethod()
or CallObjectMethod()
, where the return-type is <Primitive-type>
, void and Object
, 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 GetStaticMethodID(),
CallStatic
, 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. // Theincludes 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 includes each of the eight primitive and Object.
JNI provides a set of CallNonvirtual
functions to invoke superclass' instance methods which has been overridden in this class (similar to a super.methodName()
call inside a Java subclass):
GetMethodID()
.CallNonvirtualMethod()
, 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);
You can construct jobject
and jobjectArray
inside the native code, via NewObject()
and newObjectArray()
functions, and pass them back to the Java program.
Callback the constructor is similar to calling back method. First, get the Method ID of the constructor by passing "
" 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class TestJNIConstructor { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Native method that calls back the constructor and return the constructed object. // Return an Integer object with the given int. private native Integer getIntegerObject(int number); public static void main(String args[]) { TestJNIConstructor obj = new TestJNIConstructor(); System.out.println("In Java, the number is :" + obj.getIntegerObject(9999)); } } |
This class declares a native
method getIntegerObject()
. The native code shall create and return an Integer object, based on the argument given.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; public class TestJNIObjectArray { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // Native method that receives an Integer[] and // returns a Double[2] with [0] as sum and [1] as average private native Double[] sumAndAverage(Integer[] numbers); public static void main(String args[]) { Integer[] numbers = {11, 22, 32}; // auto-box Double[] results = new TestJNIObjectArray().sumAndAverage(numbers); System.out.println("In Java, the sum is " + results[0]); // auto-unbox System.out.println("In Java, the average is " + results[1]); } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include |
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.
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:
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.DeleteGlobalRef()
JNI function. You can create a new global reference from a local reference via JNI function NewGlobalRef()
. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class TestJNIReference { static { System.loadLibrary("myjni"); // myjni.dll (Windows) or libmyjni.so (Unixes) } // A native method that returns a java.lang.Integer with the given int. private native Integer getIntegerObject(int number); // Another native method that also returns a java.lang.Integer with the given int. private native Integer anotherGetIntegerObject(int number); public static void main(String args[]) { TestJNIReference test = new TestJNIReference(); System.out.println(test.getIntegerObject(1)); System.out.println(test.getIntegerObject(2)); System.out.println(test.anotherGetIntegerObject(11)); System.out.println(test.anotherGetIntegerObject(12)); System.out.println(test.getIntegerObject(3)); System.out.println(test.anotherGetIntegerObject(13)); } } |
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#include |
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.
ERROR MESSAGE: SEVERE: java.lang.UnsatisfiedLinkError: no xxx in java.library.path PROBABLE CAUSES: Your program uses a native library from a 3rd-party API (such as JOGL), which cannot be located in the native library search paths. POSSIBLE SOLUTION: A Java Native Library (JNI) contains non-Java library codes (in filetype of ".dll" in Windows, ".so" in Linux, ".jnilib" in MacOS). For example, JOGL's "jogl_xxx.dll", "gluegen-rt.dll". These dll's are needed for proper operations. The directory path of native libraries must be included in Java system's property "java.library.path". The "java.library.path" usually mirrors the Envrionment Variable PATH. You can list the entries by issuing: System.out.println(System.getProperty("java.library.path")); To include a directory in "java.library.path", you can use VM command-line option -Djava.library.path=pathname For JRE: > java -Djava.library.path=d:\bin\jogl2.0\lib myjoglapp For Eclipse, the VM command-line option can be set in "Run Configuration..." ⇒ "Arguments" ⇒ "VM Arguments". Alternatively, you can create a User library and specifying the native library (Refer to "Eclipse How-To") For NetBeans, the VM command-line option can be set in "Set Configuration" ⇒ "Customize..." ⇒ "Run" ⇒ "VM options".