Java calls the DLL dynamic library-JNI method implemented by C/C ++, javajni
Due to the needs of the project, I recently studied how to call the DLL in java and wrote it here for future reference:
JNI: Java Native Interface (JNI) is a part of the Java platform and can be used to interact with code written in other languages.
The following are the JNI jobs extracted from the Internet:
General description: first create a class in JAVA, generate. class through javac, and then generate. h by javah; then copy. h to VC, and implement the specific letter by VC,
After compilation, generate the DLL and put it into the JAVA project for use.
The following describes the specific steps (including instances ):
1. Create a java class: load the DLL and declare to use the DLL method. The DLL is responsible for the specific implementation. The Code is as follows:
Public class Java2cpp {static {System. loadLibrary ("javaCallcpp");} public native int DLL_ADD (int a, int B); // Add public native int DLL_SUB (int a, int B ); // subtract public native int DLL_MUL (int a, int B); // multiply public native int DLL_DIV (int a, int B ); // except public static void main (String args []) {int sum = 0; Java2cpp test = new Java2cpp (); sum = test. DLL_ADD (2, 4); System. out. println ("Java call cpp dll result:" + sum );}}
2. Generate a. h file: cmd to the Java2cpp. java directory and perform the following operations:
Step 1: javac Java2cpp. java generates java2cpp. class
Step 2: javah Java2cpp generates the Java2cpp. h header file with the following content:
Note: The content of the header file Java2cpp. h cannot be modified; otherwise, an error occurs.
3. Create a VC dynamic library: Create a C/C ++ dynamic library project named javaCallcpp, import java2cpp. h, and implement the following methods:
# Include "Java2cpp. h "# include" dllApi. h "JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1ADD (JNIEnv * env, jobject obj, jint a, jint B) {int var = 0; var = DLL_API_ADD (a, B); return var ;} JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1SUB (JNIEnv * env, jobject obj, jint a, jint B) {int var = 0; var = DLL_API_SUB (a, B); return var ;} JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1MUL (JNIEnv * env, jobject obj, jint a, jint B) {int var = 0; var = DLL_API_MUL (a, B); return var ;} JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1DIV (JNIEnv * env, jobject obj, jint a, jint B) {int var = 0; var = DLL_API_DIV (a, B); return var ;} // This file is complete
Add DLL_API_ADD (), subtract DLL_API_SUB (), multiply DLL_API_MUL (), and divide DLL_API_DIV () into other functions.
File implementation. The file name is dllApi. cpp. The implementation is as follows:
Int DLL_API_ADD (int a, int B) {return (a + B);} int DLL_API_SUB (int a, int B) {return (a-B );} int DLL_API_MUL (int a, int B) {return (a * B);} int DLL_API_DIV (int a, int B) {return (a/B );} // This file is complete
At this time, the project cannot be compiled, because the include <jni. h> error occurs, you need to add the directory where JNI is located, as shown below:
4. Compile the dynamic library project: Generate javaCallcpp. dll and copy the dynamic library to the java project directory:
5. Run the java program using DLL. The result is as follows:
So far, java has finished calling the dll.
Summary
The above section describes the JNI method of the DLL dynamic library implemented by calling C/C ++ in Java. I hope it will be helpful to you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!