The first step in development is to study the use of Java JNI, because the DLL library needs to be called in Java because of the project to do some graphics processing. Words do not say much, use the following methods:
1. Create a new Java project, say my project The new post structure is:
2, the above new project, we want to use is jtest this class, other classes are dispensable (just their original project code lazy to delete ~), wherein, we define a method name, called SayHello (), note, the front of the keyword nativie
3. Navigate to the project directory path and use the command line tool to generate the. class file. For example, my working range is in
The. h file is generated using the Javah command at this time
where each command in Java means
-classpath < paths > paths for loading classes
-D < directory > output directory
-jni generating JNI-styled header files (default)
4. At this time we can see the generated. h file in e:/
/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Com_yyh_test_jtest*/#ifndef _included_com_yyh_test_jtest#define_included_com_yyh_test_jtest#ifdef __cplusplusextern "C" {#endif/** class:com_yyh_test_jtest * Method:sayhello * Signature: () V*/Jniexportvoidjnicall Java_com_yyh_test_jtest_sayhello (jnienv*, jobject); #ifdef __cplusplus}#endif#endif
5. The Java_com_yyh_test_jtest_sayhello method is a declaration of the local Method SayHello () in the JTest class. This h file is equivalent to our interface in Java, which declares a Java_com_yyh_test_jtest_sayhello (jnienv *, Jobject) method, and then implements this method in our native method, which means we are writing C The method name used when/c++ the program must be the same as here.
6, create a DLL project---Win32 Dynamic-link Library project. Then add the generated header file com_test_testnative.h to the project, and then create a source file that references the header file and implements the functionality of the local function in the header file:
This is due to the introduction of jni.h in Com_test_testnative.h, so jni.h is added to the Include directory under the VC6.0 installation directory. Jni.h in the include in the JDK installation directory, the Jawt_md.h and jni_md.h of the two header files in the Include/win32 are also imported into the project.
Note that when using VC, you need to first copy the H file into the folder, and then use VC Import.
Note that x64 or other versions are selected, and then you can see that a DLL file was generated.
Next, Java calls the local function:
Note that the DLL path needs to be configured in path, and when configured, Eclipse needs to be restarted.
Java Invoke JNI