How Java invokes the C + + DLL library _java

Source: Internet
Author: User

Recently made a Web page face recognition project, using C + + to write face recognition algorithm, but in the Web page to use the Java background, which involves the Java call DLL problem. The following is a simple example of how to do this by looking at relevant information.

1, the first step, first in Java, a new class

As shown above, note this sentence system.loadlibrary ("Javacallcpp"), which is the code that loads the DLL file. We then need to implement the subtraction method defined below in the DLL.

2, compile the file, file name is Java2cpp.java, first compile into class file, if use Eclipse, this file has been automatically generated, in the project directory under the Bin folder. Compile with command line, open cmd window, CD to. java file directory, execute command javac Java2cpp.java, that is, generate Java2cpp.class

Then execute the command javah java2cpp generate the Java2cpp.h header file, but this step will often fail, another way to succeed, into the Eclipse project directory, into the bin folder, execute command javah-classpath. -jni the package name. Class Name (Com.test.jni.Java2cpp), and then generates the Com_test_jni_java2cpp.h

3, in VS, New project Win32 project, named: Testjni The second step is as follows:

4, the second step of the generated header file into the project folder, and then import.

5, the implementation of the method in the header file:

(1) New header file dllApi.h, code 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);

(2) New DllApi.cpp implementation of 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/a);

(3) Add code to implement Com_test_jni_java2cpp.h method in TestJNI.cpp, after adding code as follows:

//TestJNI.cpp: Defines the exported 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 an exported variable Testjni
_api int ntestjni=0;
This is an example of an exported 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; }

(4) Generate DLL, TestJNI.dll can be found in the Debug folder under the project folder, but because we require the DLL to be named Javacallcpp in Java, we then rename the project to Javacallcpp and regenerate the javacallcpp. "This step build will fail, add the path as follows"

6 Calling methods

Copy the JavaCallcpp.dll generated in step fifth into the Bin folder under the JRE installation path, and run the Java program. The results are as follows:

The above is a small set to introduce Java how to invoke the C + + DLL Library of all the narration, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.