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:
- Delare your native methods in a normal Java class, for example, HelloJNI.java.
- javac the java file(HelloJNI.java).
- javah -jni HelloJNI to generate the .h file(s).
- Write your native code, using the .h file generated in step 3; copy the function prototye from the .h file into your .c file.
- compile by using Visual Studio 2005 command shell(cl command).
- Modify your java class to load the library; recompile it; ready to run it!!!
Reference:
- http://java.sun.com/docs/books/jni/html/jniTOC.html
- http://patriot.net/~tvalesky/jninative.html
- http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html#comp
- 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
- JNI in package
No comments:
Post a Comment