Beginner guide to Java Native Interface (JNI)
, is a framework that provides a bridge between Java and native applications
Create a "c" or "cpp" program file
- ex: MessagePrinter.cpp
- compile the program
gcc MessagePrinter.cpp -o printer
./printer
- This program will print "Hello World" as output
Create a "java" program file
- ex: MessagePrinter.java
- compile the program
javac MessagePrinter.java
- generate c headers
javah MessagePrinter
- This will output
MessagePrinter.h
in the same directory
Create a new cpp program with jni header
- ex: MessagePrinter2.cpp
- Now we can create "shared lib"
- For linux:
gcc -fPIC -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux -shared -o libprinter.so MessagePrinter2.cpp
- This will output
libprinter.so
in the same directory
- For MacOS
gcc -fPIC -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/ -I/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/include/darwin/ -shared -o libprinter.dylib MessagePrinter2.cpp
- This will output
libprinter.dylib
in the same directory
Create a java program
- ex: TestClient.java
- compile the program
javac TestClient.java
- run the program
java -Djava.library.path=. TestClient
- You will get the output as "HelloWorld!"