Java features are very popular, and due to its cross-platform purpose, it has very few internal connections with local machines and limits its functions. One way to solve Java local operations is JNI.
Java calls local methods through JNI, while local methods are stored in the form of library files (on Windows platforms, DLL files and so files on UNIX machines ). By calling the internal method of the local library file, Java can achieve close connection with the local machine and call system-level interface methods.
A brief introduction and application are as follows:
I. What needs to be done in Java
In a Java program, you must declare the name of the called library in the class as follows:
Static {
System. loadlibrary ("Goodluck ");
}
Here, you do not need to write the extension name of the Library. The system determines whether the extension name is DLL or so.
You also need to make a local Declaration on the method to be called. The keyword is native. In addition, you only need to declare it, instead of implementing it. As follows:
Public native static void set (int I );
Public native static int get ();
Then compile the Java program file, generate the class, and use the javah command, JNI will generate the header file of C/C ++.
For example, the content of the program testdll. Java 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 ());
}
}
Compile it with javac testdll. Java to generate testdll. Class.
Then use javah testdll to generate the testdll. h file in the current directory. This file needs to be called by the C/C ++ program to generate the required library file.
II. C/C ++
For the generated. h header file, all C/C ++ needs to do is to implement each of its methods. Compile and connect to the database file. Copy the library file to the path of the Java program, and you can use Java to call the functions implemented by C/C ++.
Connect to the example. Let's take a look at the content of testdll. h:
/* Do not edit this file-it is machine generated */
# Include
/* Header for class testdll */
# Ifndef _ included_testdll
# DEFINE _ included_testdll
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: testdll
* Method: Get
* Signature: () I
*/
Jniexport jint jnicall java_testdll_get
(Jnienv *, jclass );
/*
* Class: testdll
* Method: Set
* Signature: (I) V
*/
Jniexport void jnicall java_testdll_set
(Jnienv *, jclass, jint );
# Ifdef _ cplusplus
}
# Endif
# Endif
In actual implementation, we only care about two function prototypes.
Jniexport jint jnicall java_testdll_get (jnienv *, jclass );
And
Jniexport void jnicall java_testdll_set (jnienv *, jclass, jint );
Here, both jniexport and jnicall are JNI keywords, indicating that this function is called by JNI. Jint is a type of communication between the Java int type and the local int type through JNI. We can ignore it and use it as an int. The function name is composed of Java _, the package path of the Java program, and the function name. Parameters, we only need to care about the parameters that exist in the Java program. As for jnienv * And jclass, we generally do not need to touch it.
Well, we will use the testdll. cpp file to implement these two functions:
# Include "testdll. H"
Int I = 0;
Jniexport jint jnicall java_testdll_get (jnienv *, jclass)
{
Return I;
}
Jniexport void jnicall java_testdll_set (jnienv *, jclass, jint J)
{
I = J;
}
Compile and connect to the library file. In this example, it is done in windows and the DLL file is generated. And the name must be the same as that to be called in Java. Here is Goodluck. dll.
Copy Goodluck. DLL to the directory testdll. Class, and run Java testdll to check the result.