Recently, I have done a project for facial recognition on the web page and wrote a facial recognition algorithm in C++. However, I have to use the Java backend on the web page, which involves the issue of java calling dlls. The following is a simple example of what the editor has achieved by looking up relevant information.
1. The first step is to create a new class in Java
As shown in the picture above, pay attention to the sentence System.loadLibrary("javaCallcpp");, which is the code to load the dll file. Then we need to implement the addition, subtraction, multiplication and division method defined below in dll.
2. Compile the file, the file name is Java2cpp.java , first compile it into the class file. If you use eclipse, the file has been automatically generated, in the bin folder in the project directory. Compile with the command line, open the cmd window, cd to the directory where the .java file is located, and execute the command javac Java2cpp.java, that is, generate Java2cpp.class
Then execute the command javah Java2cpp to generate the Java2cpp.h header file, but this step often fails. Another method can succeed. Enter the directory of the eclipse project, enter the bin folder, execute the command javah -classpath . -jni Package name.class name (com.test.jni.Java2cpp), and then generate com_test_jni_Java2cpp.h
3. Create a new project win32 in VS, name it: TestJNI, the second step is as follows:
4. Copy the header file generated in the second step into the project folder and then import it.
5. Implement the method in the header file:
(1) Create a new header file dllApi.h, the code is as follows:
#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);
(2) Create a new 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);} (3) Add code to TestJNI.cpp to implement the com_test_jni_Java2cpp.h method. After adding the code is as follows:
// TestJNI.cpp: Defines the export function of the DLL application. //#include "stdafx.h"#include "TestJNI.h"#include "com_test_jni_Java2cpp.h"#include "dllApi.h"// This is an example of exporting variables TESTJNI_API int nTestJNI=0;// This is an example of exporting functions. TESTJNI_API int fnTestJNI(void){ return 42;}// This is the constructor of the exported class. // For information about class definitions, see TestJNI.hCTestJNI::CTestJNI(){ return;}JNIEXPORT jint JNICALL Java_com_test_jni_Java2cpp_DLL_1ADD(JNIEnv *env, job 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, job 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, job 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, job obj, jint a, jint b){ int var = 0; var = DLL_API_DIV(a, b); return var;}(4) Generate dll, and you can find TestJNI.dll in the Debug folder under the project folder. However, because we require the dll to be named JavaCallcpp in Java, we rename the project to JavaCallcpp at this time, and then regenerate JavaCallcpp. [This step will fail to generate, add the path as follows]
6 Calling methods
Copy the JavaCallcpp.dll generated in step 5 into the bin folder under the JRE installation path and run the java program. The results are as follows:
The above is the full description of how Java calls the C++ DLL library that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!