1. Create a java file (HelloWorld. java ). ClassHelloWorld {static {System. loadLibrary ("HelloWorld");} privatenativevoidprint (); publicstaticvoidmain (Stringargs []
1. Create a java file (HelloWorld. java ).
Class HelloWorld {
Static {
System. loadLibrary ("HelloWorld ");
}
Private native void print ();
Public static void main (String args []) {
New HelloWorld (). print ();
}
}
2. Use the javac command to compile the java file and enter "javac HelloWorld. java" on the terminal ". Generate HelloWorld. class.
3. Use the javah command to generate the header file. Enter "javah-jni HelloWorld" in the terminal to generate HelloWorld. h
4. Use the local method to implement the method in the header file.
# Include
# Include
# Include "HelloWorld. h"
JNIEXPORT void JNICALL Java_HelloWorld_print
(JNIEnv * env, jobject obj)
{
Printf ("HelloWorld JNI! \ N ");
Return;
}
5. compile and generate the so file with gcc.
Gcc-shared-I/home/xl/java/jdk1.5.0 _ 22/include-I/home/xl/java/jdk1.5.0 _ 22/include/linux-I/usr/include HelloWorld. c-o libHelloWorld. so