Contents
Java supports native codes via the Java Native Interface (JNI).
1. JNI with C
1.1: Write a Java Class that uses C Codes – hello.java 1
1.2: Create the C/C++ Header file – hello.h 2
1.3: C Implementation – HelloJNI.c 2
1.4: Run the Java Program 3
1. JNI with C
1.1: Write a Java Class that uses C Codes – hello.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ nano hello.java public class hello { static { System.loadLibrary ( "hello" ) ; } public native void sayHello(int length) ; public static void main (String args[]) { String str = "I am a good boy" ; hello h = new hello () ; h.sayHello (str.length() ) ; } } |
The static initializer invokes System.loadLibrary()
to load the native library “hello
” (which contains the native method sayHello()
) during the class loading. It will be mapped to “libhello.so
“. You could include the library into Java Library’s path via JVM argument -Djava.library.path=path_to_lib.
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()
is contained in the native library loaded.
The main()
method allocate an instance of hello and invoke the native method sayHello()
.
Compile the “hello.java” into “hello.class”.
1 2 3 4 5 |
$ javac hello.java $ ls -l total 8 -rw-rw-r-- 1 jack jack 527 พ.ย. 24 16:02 hello.class -rw-rw-r-- 1 jack jack 294 พ.ย. 24 16:01 hello.java |
1.2: Create the C/C++ Header file – hello.h
(get .h)
1 2 3 4 5 6 |
$ javah hello $ ls -l total 12 -rw-rw-r-- 1 jack jack 527 พ.ย. 24 16:02 hello.class -rw-rw-r-- 1 jack jack 365 พ.ย. 24 16:02 hello.h -rw-rw-r-- 1 jack jack 294 พ.ย. 24 16:01 hello.java |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$ cat hello.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class hello */ #ifndef _Included_hello #define _Included_hello #ifdef __cplusplus extern "C" { #endif /* * Class: hello * Method: sayHello * Signature: (I)V */ JNIEXPORT void JNICALL Java_hello_sayHello (JNIEnv *, jobject, jint); #ifdef __cplusplus } #endif #endif |
1.3: C Implementation – HelloJNI.c
1 2 3 4 5 6 7 8 |
$ nano hello.c #include<stdio.h> #include<jni.h> #include "hello.h" JNIEXPORT void JNICALL Java_hello_sayHello (JNIEnv *env, jobject object, jint len) { printf ( "Length is %d\n", len ); <span style="line-height: 1.71429; font-size: 1rem;">}</span> |
(get .o)
1 2 3 4 5 6 7 8 |
$ gcc -fPIC -c hello.c -I $JAVA_HOME/include jack@jack14043:~/JNI2$ ls -l total 20 -rw-rw-r-- 1 jack jack 179 พ.ย. 24 16:04 hello.c -rw-rw-r-- 1 jack jack 527 พ.ย. 24 16:02 hello.class -rw-rw-r-- 1 jack jack 365 พ.ย. 24 16:02 hello.h -rw-rw-r-- 1 jack jack 294 พ.ย. 24 16:01 hello.java -rw-rw-r-- 1 jack jack 1584 พ.ย. 24 16:04 hello.o |
(get .so)
1 2 3 4 5 6 7 8 9 |
$ gcc hello.o -shared -o libhello.so -Wl,-soname,hello $ ls -l total 28 -rw-rw-r-- 1 jack jack 179 พ.ย. 24 16:04 hello.c -rw-rw-r-- 1 jack jack 527 พ.ย. 24 16:02 hello.class -rw-rw-r-- 1 jack jack 365 พ.ย. 24 16:02 hello.h -rw-rw-r-- 1 jack jack 294 พ.ย. 24 16:01 hello.java -rw-rw-r-- 1 jack jack 1584 พ.ย. 24 16:04 hello.o -rwxrwxr-x 1 jack jack 8019 พ.ย. 24 16:06 libhello.so |
1.4: Run the Java Program
1 2 |
$ java -Djava.library.path=. hello Length is 15 |