Java: Getting Started with JNI Configuration [Very Simple]

We all know that Java cannot directly call the hardware driver of the operating system, while C language is possible. So we envisage to implement the operation of the hardware driver by calling C language functions through Java.

The simple configuration is as follows:
download visual studio2015. (Since VC does not support the generation of 64-bit dll files, it can only be generated by visual studio2015).

The configuration of Java is relatively simple. Put jni.h in H:\Program Files (x86)\Java\jdk1.8.0_92\include in the Java directory and jawt_md in H:\Program Files (x86)\Java\jdk1.8.0_92\include\win32. h and jni_md.h are copied to the Microsoft Visual Studio 14.0\VC\include directory
[External link image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-uvmqgpSP-1614134130952) (https: //img-blog.csdn.net/20160801213347040)]

We open eclipse and write the following code:

public class MainClass {

	static {
		System.loadLibrary("JNI_HelloWorld");
	}
	
	private native void display();
	
	public static void main(String[] args) {
		new MainClass().display();
	}
}

The local method display() is declared; that is to say, Java declares the method, and C and C++ languages ​​implement the method.
We use the javah command line to generate C language header files. The javah command is used for the .class file, which means we have to compile the .java file first

javac MainClass.java  // MainClass.java是我们刚编写的代码
javah MainClass       // MainClass是.class的文件名

After running, the MainClass.h file will be generated. [External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-D17AxFVP-1614134130955)(https://img-blog.csdn.net/20160801214011390)]

We created a new JNI_HelloWorld project in visual studio
[External link image transfer failed, the source site may have an anti-leech chain mechanism, it is recommended to save the image and upload it directly (img-0uYIKh7K-1614134130958)(https://img-blog.csdn.net/ 20160801213639792)]
[image dump outer link fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-7hHY2vAO-1614134130959) ( https://img-blog.csdn.net/20160801213709043)]
Since the The 64-bit dll file generated by visual studio needs to be configured (32-bit is generated by default) and the
following settings:
[External link image transfer failed, the source site may have anti-theft link mechanism, it is recommended to save the image and upload it directly (img-w0YsZGIy-1614134130960 )(https://img-blog.csdn.net/20160801214228293)]
Right-click the project and select properties. Modified as follows:
[image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-mBFWnNHg-1614134130962) ( https://img-blog.csdn.net/20160801214310583)]
so The generated dll is 64-bit.

When we click to run the project, we will find that an x64 folder will be generated in the project
[External link image transfer failed, the source site may have an anti-theft chain mechanism, it is recommended to save the image and upload it directly (img-iMXzOlUp-1614134130963) (https: //img-blog.csdn.net/20160801214433263)]
We open the JNI_HelloWorld folder in the project directory. Copy MainClass.h to this directory.
[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-9vnTABsq-1614134130964)(https://img-blog.csdn.net/20160801214542446)]
Then in visual studio Create a new cpp file. The file name can be taken at will.
Write the following code:

#include <jni.h>
#include "MainClass.h"
#include <iostream>
using namespace std;

JNIEXPORT void JNICALL Java_MainClass_display
(JNIEnv *, jobject) {
	cout << "HelloWorld\n";
	return;
}

We click Run. The dll file will be generated. The file is in the x64/Debug folder.
We copy the file into eclipse and run the code again. It will output HelloWorld. If it appears

Exception in thread "main" java.lang.UnsatisfiedLinkError: no JNI_HelloWorld in java.library.path

Questions, you can check the path.

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/52089460