Java, which is popular with its cross-platform features, and because of its cross-platform purpose, makes it less powerful and its internal connections to local machines, constraining its functionality.
One way to solve Java's local operations is JNI.
Java invokes the local method through JNI, and the local method is stored in the form of a library file (in the form of a DLL file on a Windows platform and a so file form on a UNIX machine). By invoking the internal method of the local library file, Java can implement close contact with the local machine and invoke the system-level interface methods.
Simple introduction and application are as follows:
One, Java needs to do the work
In a Java program, you first need to declare the name of the library called in the class, as follows:
static {
System.loadLibrary(“goodluck”);
}
Here, the extension of the library name can not be written out, whether it is a DLL or so, by the system itself to judge.
You also need to make a local declaration of the method that will be invoked, with the keyword native. And only need to declare, and not need to implement specifically. As follows:
Public native static void set (int i);
public native static int get ();
Then you compile the Java program file, generate class, and then use the Javah command, and JNI will generate the header file for C + +.
For example, program Testdll.java, the content is:
public class testdll
{
static
{
System.loadLibrary("goodluck");
}
public native static int get();
public native static void set(int i);
public static void main(String[] args)
{
testdll test = new testdll();
test.set(10);
System.out.println(test.get());
}
}
Compiling it with Javac Testdll.java generates Testdll.class.
With Javah Testdll, the testdll.h file will be generated in the current directory, which needs to be called by C + + program to generate the required library files.