A recent project to develop web-side face recognition projects, face recognition algorithm has been written, is the C + + version, but the web side requires the use of Java background, which involves the Java call DLL problem. After searching, a simple example is implemented.
1, the first step, first in Java to create a new class
For example, note the phrase system.loadlibrary ("javacallcpp"), which is the code that loads the DLL file. Then we need to implement the subtraction method defined below in the DLL.
2, compile the file, the file name is Java2cpp.java, first compiled into a class file, if the use of Eclipse, this file has been automatically generated, in the project directory under the Bin folder. Compile with command line, open cmd window, CD to the directory where the. java file is located, execute command javac Java2cpp.java, generate Java2cpp.class
Then execute the command javah java2cpp generate Java2cpp.h header file, but this step will often fail, another method can be successful, into the Eclipse project directory, into the bin folder, execute command javah-classpath. -jni the package name, the class name (Com.test.jni.Java2cpp), and then generates the Com_test_jni_java2cpp.h
3. New project Win32 Project in VS, named: Testjni The second step is as follows:
4. Copy the header file generated in the second step into the project folder and import it.
5, implement the method in the header file:
(1) New header file dllApi.h, the code is as follows:
" Com_test_jni_java2cpp.h " int Dll_api_add (intint b); int Dll_api_sub (intint b); int Dll_api_mul (intint b); int Dll_api_div (intint b);
(2) New DllApi.cpp implementation of the above method, the code is as follows:
#include"stdafx.h"#include<iostream>#include"dllApi.h"intDll_api_add (intAintb) {return(A +b);}intDll_api_sub (intAintb) {returnAb);}intDll_api_mul (intAintb) {return(A *b);}intDll_api_div (intAintb) {returnAb);}
(3) Add code to implement the Com_test_jni_java2cpp.h method in TestJNI.cpp, add the following code:
//TestJNI.cpp: Defines an export function for a DLL application. //#include"stdafx.h"#include"TestJNI.h"#include"Com_test_jni_java2cpp.h"#include"dllApi.h"//This is an example of exporting a variableTestjni_apiintNtestjni=0;//This is an example of an exported function. Testjni_apiintFntestjni (void) {return the;}//This is the constructor for 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, 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;}
(4) Generate DLL, TestJNI.dll can be found in the Debug folder under the project folder, but since we require the DLL to be named Javacallcpp in Java, rename the project to Javacallcpp at this point and regenerate javacallcpp. "This step generation will fail, add the path as follows"
6 Calling methods
Copy the fifth step generated JavaCallcpp.dll into the Bin folder under the JRE installation path and run the Java program. The results are as follows:
Java calls C + + DLL Library method