In the project now, we use the DLL file of the access device that JNI calls the C language implementation, before this is a Greek word for jni, only heard of the JNA link DLL, but only an understanding.
JNI (Java Native Interface) Java local interface, the main role is to implement Java code and C C + + code to interact with.
Theory is not much to say, or look at the Java link DLL specific operation bar. As follows:
The following instance calls the open () method in the Connection.dll file in Java.
1,java Side
1) Loading DLL file
There are two ways of System.load (); and system.loadlibrary ();
The load requires a full path and requires a file name extension;
LoadLibrary is a specific path, either under the system system32 or under the Project Bin directory, as long as the file name does not have a. dll extension.
2) Local Declaration method
When the DLL file is loaded, the method that will be used is declared with the native keyword.
<span style= "FONT-SIZE:18PX;" > Public
class condll{
static{
system.load ("D://connection.dll");
or system.loadlibrary ("Connection");
}
Public native static void open ();
} </span>
3) Generate. h header File
Use the Javah condll command to generate the. h header file (use Javac to generate the. class file first).
#include <stdio.h>
#include <ConDll.h>
jniexport void Jnicall java_condll_open (jnienv *env, Jobject obj);
The method in the generated header file is named the full path and method of the Java class, separated by an underscore in the middle.
Note: If you call the existing DLL file in the project, we must know the declaration of the method in C, and create the corresponding package structure in the Java program, if the path does not correspond, run the Java program will be the error, hands-on lessons. (I did a project to adjust the package name, DLL file did not recompile).
2,c Side
All methods are implemented according to the generated. h header file.
In a generic project, DLL files are available first, and in Java you must ensure that the package path and class name of the Java class are the same as the method full path for the DLL.