Tuesday, August 30, 2011

JNI Refreshment

Today one of my colleague asked me some questions on JNI implementation. The knowledge seems fading away from me. To avoid it happens again, I recorded the basic steps:
  1. Delare your native methods in a normal Java class, for example, HelloJNI.java.
  2. javac the java file(HelloJNI.java).
  3. javah -jni HelloJNI to generate the .h file(s).
  4. Write your native code, using the .h file generated in step 3; copy the function prototye from the .h file into your .c file.
  5. compile by using Visual Studio 2005 command shell(cl command).
  6. Modify your java class to load the library; recompile it; ready to run it!!!
Reference:
  1. http://java.sun.com/docs/books/jni/html/jniTOC.html
  2. http://patriot.net/~tvalesky/jninative.html
  3. http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html#comp
  4. command to compile your c file: c:\test\jnitest>cl /I"C:\Program Files (x86)\Java\jdk1.6.0_24\include" /I"C:\Program Files (x86)\Java\jdk1.6.0_24\include\win32" -LD HelloJNI.c -Felibnative.dll
  5. JNI in package