Java is not perfect, the lack of Java in addition to the speed of running than the traditional C + + slower than many,
Java does not have direct access to the underlying operating system (such as system hardware),
For this purpose Java uses the native method to extend the functionality of the Java program .
The native method can be likened to the Java program's interface to the C program, and its implementation steps:
1, declare the native () method in Java, and then compile;
2. Create an. h file with Javah;
3, write a. cpp file to implement the native export method, which needs to include the second step produced by the. h file (Note that it also contains the JDK jni.h file);
4, the third step of the. cpp file is compiled into a dynamic link library file;
5, in Java with the System.loadlibrary () method to load the fourth step generated by the dynamic link library file, the native () method can be accessed in Java.
Scenarios where Java native methods are applicable
1. In order to use one of the features of the underlying host platform, this feature cannot be accessed through the Java API
2. In order to access an old system or use an existing library, this system or library is not written in Java
3. To speed up the performance of the program, a period of time-sensitive code is implemented as a local method.
First, write the Java file.
Package com.hode.hodeframework.modelupdate;
public class Checkfile
{
public native void Displayhelloworld ();
Static
{
System.loadlibrary ("test");
}
public static void Main (string[] args) {
New Checkfile (). Displayhelloworld ();
}
}
Then compile the class file according to the written file.
Then execute Javah-jni com.hode.hodeframework.modelupdate.CheckFile in the class root directory such as classes or bin,
You get a com_hode_hodeframework_modelupdate_checkfile.h file in the root directory.
Then write the com_hode_hodeframework_modelupdate_checkfile.c file based on the contents of the header file
#include "CheckFile.h"
#include
#include
Jniexport void Jnicall Java_com_hode_hodeframework_modelupdate_checkfile_displayhelloworld (JNIEnv *env, Jobject obj)
{
printf ("Hello world!\n");
Return
}
After compiling the build DLL file such as "Test.dll", the name matches the name in System.loadlibrary ("test")
VC Compilation Method: Cl-i%java_home%\include-i%java_home%\include\win32-ld com_hode_hodeframework_modelupdate_checkfile.c- Fetest.dll
At the end of the run, add the parameter-djava.library.path=[dll the path of the store]
Java Native (GO)