Step 1:
Compile Java class, e.g .:
// Filename: helloworld. javapublic class helloworld {public native void displayhelloworld (); Public native int add (int A, int B); static {system. loadlibrary ("hello");} public static void main (string [] ARGs) {helloworld hW = new helloworld (); HW. displayhelloworld (); system. out. println (); system. out. println ("1 + 20 =" + HW. add (1, 20 ));}}
Step 2: javac helloworld. Java
Step 3: javah helloworld
The generated header file is as follows:
/* Do not edit this file-it is machine generated */# include <JNI. h>/* Header for class helloworld */# ifndef _ included_helloworld # DEFINE _ included_helloworld # ifdef _ cplusplusextern "C" {# endif/** class: helloworld * method: displayhelloworld * Signature: () V */jniexport void jnicall java_helloworld_displayhelloworld (jnienv *, jobject);/** class: helloworld * method: Add * Signature: (ii) I */jniexport jint jnicall java_helloworld_add (jnienv *, jobject, jint, jint); # ifdef _ cplusplus} # endif
Step 4: Use vc6 to create a Win32 DLL project named hello
Step 5:
Tools-> Option-> directory (Label)
Directories adds two include paths (For details, refer to the actual include paths of Java ):
D: \ Java \ JDK \ jdk1.6.0 _ 21 \ include
D: \ Java \ JDK \ jdk1.6.0 _ 21 \ include \ Win32
Step 6:
Create hello. cpp
# include
# include "helloworld. H "# include
# include
jniexport void jnicall java_helloworld_displayhelloworld (jnienv *, jobject) {printf ("Hello China! @ # $ ");}/** Class: helloworld * method: Add * Signature: (ii) I */jniexport jint jnicall java_helloworld_add (jnienv *, jobject, jint, jint B) {return a + B ;}