JNI development minimalist tutorial

Keywords: Java Eclipse JDK Attribute

JNI(Java Native Interface)

Java calls C/C + +, C/C + + calls a set of Java API

Preface:

Learning JNI requires knowledge of C/C + + and JNI manual
Students who want to learn, please follow my study notes NDK development learning notes

1: write native methods in eclipse


public class Java2cpp {
    
    static { 
    System.loadLibrary("Project4");
    } 

    public native int DLL_ADD(int a,int b);//plus
public native int DLL_SUB(int a,int b);//reduce
public native int DLL_MUL(int a,int b);//ride
public native int DLL_DIV(int a,int b);//except

    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.javah command to generate. h header file

  • Step 1: javac Java2cpp.java generates java2cpp.class

  • Step 2: javah Java2cpp generates Java2cpp.h header file, which is as follows:

    cmd operation

//Generated C + + header file Java2cpp.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class Java2cpp */

#ifndef _Included_Java2cpp
#define _Included_Java2cpp
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Java2cpp
 * Method:    DLL_ADD
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1ADD
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     Java2cpp
 * Method:    DLL_SUB
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1SUB
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     Java2cpp
 * Method:    DLL_MUL
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1MUL
  (JNIEnv *, jobject, jint, jint);

/*
 * Class:     Java2cpp
 * Method:    DLL_DIV
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1DIV
  (JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

3. Copy. H header file, jni.h and jni_md.h to CPP project

  • Create a new empty C + + project with visual studio, and introduce the header file Java2cpp.h. because there is no jni.h and JNI? Md.h, we also need to import (jni.h and JNI? Md.h are in the lib of jdk),


    Project structure

4. Implement the functions declared in the. h header file

Part of the code is as follows

#include "Java2cpp.h"

/*
* Class:     Java2cpp
* Method:    DLL_ADD
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1ADD
(JNIEnv *env, jobject obj, jint a, jint b){
    int var = 0;
    var = a + b;
    return var;
}

/*
* Class:     Java2cpp
* Method:    DLL_SUB
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_Java2cpp_DLL_1SUB
(JNIEnv *env, jobject obj, jint a, jint b){
    int var = 0;
    var = a-b;
    return var;
}

5. Generate dll file

  • Configuration file, right-click the item structure selection attribute in Figure 3

    Picture description here

  • Just build the solution

6. Configure the directory of dll file to environment variable

Configure the directory of dll file to Path, for example, I put dll in D:\DllTools,
You need to add D:\DllTools; to the front of the path

7. Restart Eclipse

Restart to view results


public class Java2cpp {
    
    static { 
    System.loadLibrary("Project4");
    } 

    public native int DLL_ADD(int a,int b);//plus
public native int DLL_SUB(int a,int b);//reduce
public native int DLL_MUL(int a,int b);//ride
public native int DLL_DIV(int a,int b);//except

    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);  
    }
    
}

//Results displayed by console

Java call cpp dll result:6

Posted by bealers on Sat, 14 Dec 2019 07:28:06 -0800