Step 1: Create a new class in Java

   

See above, where notice the sentence system. loadLibrary(“javaCallcpp “); , this is the code to load the DLL file. Then we need the DLL to implement the addition, subtraction, multiplication and division methods defined below.

2, compile the file named java2cpp.java, first to the class file, if you are using Eclipse, this file is automatically generated, in the project directory bin folder. Open the CMD window, CD to the directory where the. Java file is stored, and run the javac java2cpp. Java command to generate java2cpp. class

Then execute the javah Java2cpp command to generate the java2cpp.h header file, but this step often fails. Another method can succeed, go to the Eclipse project directory, go to the bin folder, and execute the javah-classpath. -jni package name. Class name (com.test.jni.java2cpp) and then generate com_test_jni_java2cpp.h

Create a win32 project in VS and name it TestJNI

4. Copy the header generated in step 2 to the project folder and import it.

5, implement the method in header file:

(1) Create a new header file dllapi.h with the following code:

#include "com_test_jni_Java2cpp.h" int DLL_API_ADD(
                                                        int a, int b);
int DLL_API_SUB(int a, int b);
int DLL_API_MUL(int a, int b);
int DLL_API_DIV(int a, int b);Copy the code

 

(2) Create dllapi.cpp to implement the above method. The code is as follows:

#include "stdafx.h" #include <iostream> #include "dllapi.h" 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); }Copy the code

 

Com_test_jni_Java2cpp. H = com_test_jni_Java2cpp.

// testjni. CPP: defines the DLL application export function. H "#include" testjni. h" #include "com_test_jni_java2cpp. h" #include" dllapi.h "// This is an example of exporting a variable TESTJNI_API int nTestJNI=0; // This is an example of an export function. TESTJNI_API int fnTestJNI(void) {return 42; } // This is the constructor of the exported class. // For information about class definitions, see testjni.h CTestJNI::CTestJNI() {return; } JNIEXPORT jint JNICALL Java_com_test_jni_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_com_test_jni_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_com_test_jni_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_com_test_jni_Java2cpp_DLL_1DIV(JNIEnv *env, jobject obj, jint a, jint b){int var = 0;  Var = DLL_API_DIV (a, b); return var; }Copy the code

 

Testjni. DLL can be found in the Debug folder under the project folder, but because we require the DLL name JavaCallcpp in Java, we will rename the project to JavaCallcpp, and then generate JavaCallcpp again. [This step will fail to generate, add path as follows]

6 Calling methods

Copy the Javacallcpp. DLL generated in Step 5 to the bin folder in the JRE installation path and run the Java program. The results are as follows: