Build the Environment
- Installing the Java SDK and adding environment variables
- Installing Elipse Java and CDT plugins
- Install TDM64-GCC and add environment variables Baidu Cloud (password: MJDI)
- Install Msys and add environment variables Baidu Cloud (password: j2i6)
JNI use steps to create a Java engineering Jni_demo
Create the class Jnidemo and write the following:
package jni_demo;public class JNIDemo { public native void sayHello(); public static void main(String[] args) { // 调用动态链接库 System.loadLibrary("libJNIDemo"); JNIDemo jniDemo = new JNIDemo(); jniDemo.sayHello(); }}
Open the command line, enter the SRC path of the Java project, and enter the following command to generate the local method header file:
Create a dynamic library Jnidemo
Project header file configuration:
Create the SRC directory, move the generated header files to the SRC directory, and create the corresponding. c files, and write the following:
#include "jni_demo_JNIDemo.h"JNIEXPORT void JNICALL Java_jni_1demo_JNIDemo_sayHello (JNIEnv *env, jobject obj){ printf("Hello Java, I am C.");}
Project right: Build project, generate dynamic library LibJNIDemo.dll
Java Call DLL
Create the Sibling directory lib in the SRC directory of the Jni_demo project, move the LibJNIDemo.dll to the directory, and configure the Dynamic library path:
The final project structure is as follows:
Run the Java project and output the following results to prove that the call was successful:
Sample Code Address
Baidu Cloud (password: qyq4)
Reference links
Eclipse calls the setup of the JNI environment
Teach you how to invoke JNI full strategy through Eclipse engineering configuration
In this paper, according to the above two articles combined with their own development environment, the implementation of the JNI call, and provide sample code, for reference only.
Eclipse JNI Environment Setup and use