Recently, the technology is designed to JNI technology. For VC , I know half-solving. JNI is the interface of the Native method, never used it. But I touched it, but I feel a bit road, I'm getting the jni problem, for myself The little advancement did not dare to conceal, and the brothers who were not open to giving them a reference.
JNI call DLL is used in JAVA
1 Write an example document
Public class testnative {
Private Native Int Add (int X, int y);
Public static void main (String [] args) {
Testnative HH = New Testnative ();
INT r = HH.Add (30, 20);
System.out.println ("Result =" R);
}
STATIC {
System.loadLibrary ("DLL_0305");
}
}
among them
The DLL file name in LoadLibrary can be cascaded. As long as the Dynamic Link Library is changed to this string. The source of the Native function is the dynamic link library.
2. After the step is prepared, use Javac Testnative.java to generate .CLASS. Class. Testing syntax.
3. After the syntax check, use the Javah Testnative to generate .h C header file.
4. Open VC 6.0, New -> Enginee -> Win32 Dynamic-Link Library. Select an empty project in the wizard.
5. Add the header file that Javah generated to the project. Then the file -> New-> TextFile is a .c file. Implement your own Native function. The list is as follows:
#include
JNIEXPORT JINT JNICALL JAVA_TESTNATIVE_ADD
(JNIENV * EV, Jobject Obj, JINT X, JINT Y)
{
Return X Y;
}
Remarks:
A jni.h usually in the JDK / include / you can copy it to the include directory in the VC installation directory. One is eternal, and JNI technology can be easily used in the future.
B All data types need to consider the interface issues of Java and VC. Use jint jstring, etc.
C function name and automatically generated. The name of the naming rule is: java_ use class _ method name
6 fully compiled projects.
If there is no error, the dynamic link library is successful at this time.
Copy the dynamic link library to the directory where the testnative.java is located.
7 Watch the Java Testnative Look at the results.