1, first in Java to write a simple class
public class Hello
{public
native void SayHello ();
static {
system.loadlibrary ("Hellodll");
}
public static void Main (string[] args) {
new Hello (). SayHello ();
}
}
Compiling the class using Javac Hello.java
Then use Javah hello to generate the Hello.h header file
2. Create a new dynamic link library project in Visual C + + 6.0, project named Hellodll
3. In the project, a new header file named: hello.h, copy the code in the Hello.h file just generated, as follows:
/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for class Hello/*/
#i Fndef _included_hello
#define _included_hello
#ifdef __cplusplus
extern "C" {
#endif/
*
* Class: Hello
* Method: SayHello
* Signature: () V
/
jniexport void Jnicall Java_hello_sayhello
(jnienv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
No need to make any changes
At this point, create a new Test.cpp file to implement the function of the header file just now, the code is as follows
#include "Hello.h"
#include <jni.h>
jniexport void Jnicall Java_hello_sayhello
(jnienv *, Jobject) {
printf ("Hello world,i ' m coming!");
return;
}
At this point if the compilation will be an error, said that the jni.h file can not be found, as follows:
Fatal error C1083:cannot open include file: ' jni.h ': No such file or directory
Only step 4 can be done at this point
4. Copy the Jni_md.h files under Jni.h and ~\jdk1.6.0_25\include\win32 to Visual C + + 6.0 in the JDK installation directory ~\jdk1.6.0_25\include D:\Microsoft In the Visual studio\vc98\include directory
5, at this time in the compilation of linked Test.cpp files, you can generate dynamic link library files:
Then copy the file to the same directory as the Hello.class file, run the Java file, you can invoke the interface of C/E + + writing implementation, that is, dynamic link library file
The results of the operation are as follows:
This calls success.