This repository offers sample builds supporting native code through the Java Native Interface (JNI).
-
Create run directory using the mkdir command as directory name
/sample$ mkdir sample $ cd sample -
Write a Java Class CalculateMath.java that uses C Codes
$ touch CalculateMath.java -
You should use
javac -hto compile the Java program and generate C/C++ header file called CalculateMath.h as follows:(In the parent directory)$ javac -h . sample/CalculateMath.java -
Create and implementing the C Program CalculateMath.c
$ touch CalculateMath.c -
Compile the C program CalculateMath.c
$ gcc -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" -shared -o libmathlib.so CalculateMath.c -
Native library can be loaded by loadLibrary with a valid name and run java program.
$ static { System.loadLibrary("mathlib"); // will load libmathlib.so }