JNI Basics
From Weiss's
C++ for Java Programmers
Initial Code:
To run the code
- Compile the Java Code
javac HelloNativeTest.java
This creates two .class files: HelloNativeTest.class, HelloNative.class
- The HelloNative.class file is used to create the proper .h file for the C++
code by using the command of:
javah HelloNative
- Now to compile the C++ code (shown using UNIX commands):
# g++ -c -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/linux HelloNative.cpp
g++ -c -fPIC HelloNative.cpp
This create the HelloNative.o file.
- Now create the shared library object file with the command:
g++ -shared -o libHelloNative.so HelloNative.o
- Before running the program the java code has to know where
the shared library file is located so it can be loaded, so we
need to set the LD_LIBRARY_PATH environment variable to its
locations. The command below assumes the file is in the current
directory.
setenv LD_LIBRARY_PATH .
- Now to run the java program with
java HelloNativeTest
Other Examples from Weiss