Due to the needs of the project, I have recently studied the method of java calling DLL. How to call it is written here for easy reference in the future:
The method adopted is JNI: Java Native Interface, referred to as JNI, is part of the Java platform and can be used to interact with code written in Java in other languages.
Below is a schematic diagram of JNI work extracted from the Internet:
Overall description: First create a class in JAVA, generate .class through javac, and then generate .h from javah; then copy .h to VC, and implement specific functions by VC.
After compiling and passing, generate DLL, put the DLL into the JAVA project and use it, and it is finished.
Let’s talk about the specific steps (including examples):
1. Build a java class: load DLL, declare that DLL method should be used, and the specific implementation is responsible for DLL; 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); //Decrease public native int DLL_MUL(int a,int b); //Multiple public native int DLL_DIV(int a,int b); //Divide 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 .h file: cmd to the Java2cpp.java directory and do the following:
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 will occur.
3. Making VC dynamic library: Create a C/C++ dynamic library project, named javaCallcpp, import java2cpp.h and implement its method:
#include "Java2cpp.h"#include "dllApi.h"JNIEXPORT jint JNICALL Java_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_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_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_Java2cpp_DLL_1DIV(JNIEnv *env, job obj, jint a, jint b){int var = 0;var = DLL_API_DIV(a,b);return var;} //This file is finishedAdd DLL_API_ADD(), subtract DLL_API_SUB(), multiply DLL_API_MUL(), and divide DLL_API_DIV().
Implemented in the file, the file name is dllApi.cpp, and 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 (ab);}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 finishedAt this time, the project still compiles, because there is an error in include<jni.h>, you need to add the directory where the JNI is located, as follows:
4. Compile the dynamic library project: generate javaCallcpp.dll and copy this dynamic library to the java project directory:
5. Use DLL: Run the java program, the result is as follows:
At this point, the java call dll has been completed.
Summarize
The above is the method of Java dynamic library - JNI implemented by calling C/C++ that the editor introduces 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!