“Hello World” JNI

Steps to be follow to compile a hello world program on ubuntu

1) Create java file    — MyPro.java

2) Compile with javac
javac MyPro.java
output:- MyPro.class

3) Generate .h file for native cpp class using javah
javah -jni MyPro ( class name generated after javac but donot mention “.class”)
output :- MyPro.h (donot change it).

4) Create c file including this MyPro.h in your source code(nativelib.c).

5) Compile and create .so shared library(I am using ubuntu 10.04)
g++ –shared -o libnativelib.so -I/usr/lib/jvm/java-6-sun/include/ -I/usr/lib/jvm/java-6-sun/include/linux nativelib.c -fPIC
output:- libnativelib.so

g++ -fPIC –shared -o libnativelib.so -I<path to java>/jvm/java-6-sun/include/ -I<path to java>/jvm/java-6-sun/include/linux <source file of c/cpp>

6 Interprate the .class file using java command like
java -Djava.library.path=.  MyPro
java -Djava.library.path=. <name of the generated .class name in step 2>


References: StackOverflow

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s