1. New java code HelloWorld. java
Class HelloWorld {
Public native void displayHelloWorld ();
Static {
System. loadLibrary ("hello ");
}
Public static void main (String [] args ){
New HelloWorld (). displayHelloWorld ();
}
} Www.2cto.com
2. javac HelloWorld. java
3. javah-jni HelloWorld generates the c ++ header file HelloWorld. h
/* Do not edit this file-it is machine generated */
# Include <jni. h>
/* Header for class HelloWorld */
# Ifndef _ Included_HelloWorld
# Define _ Included_HelloWorld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: () V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject );
# Ifdef _ cplusplus
}
# Endif
# Endif
4. Implement the header file HelloWorld. h In the c ++ header file HelloWorld. cpp
# Include "HelloWorld. h"
# Include <iostream>
Using namespace std;
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject ){
Printf ("this is my jni hello, world \ n ");
Cout <"hello, world! "<Endl;
}
5. compile and generate the hello. dll dynamic library
Tool --> Option --> source control --> include files include the header files under jdk --> include and win32 header files.
6. run java HelloWorld