Tuesday, April 21, 2009

How to use JNI in Linux

Java Native Interface allows you to call functions written in different programming languages from java code. For eg C, C++.
Steps involved:
  1. Create your java class & declare the method to be called using JNI with the native keyword.
  2. Compile the java program (javac FileName.java)
  3. Create the C or C++ header (javah -jni FileName) , FileName.h is generated by the machine.
  4. Write your C or C++ program and include the above generated header file.
  5. Create the shared object file (.so) (gcc -path_where_jdk_is_located/jdk1.6/include -path_where_jdk_is_located/jdk1.6/include/linux FileName.c -shared -o filename.so)
Note : Be sure to specify the same "filename.so" in the java program as well...

No comments:

Post a Comment