JNI Complete Manual (1)
From Yippit
Recently, I have a mobile phone project in the company, requiring Java programs to connect to a third-party SMS server when sending text messages. The SMS interface is written with C . Three days, gave a general understanding of the main part of JNI. First, I will finish my heart, I hope all my friends will go less. Firstly, an article introduces a process of simple JNI calls. Java is very popular with its cross-platform features, but it is due to its cross-platform purpose, making it very little internal contacts and local machines, constrained its functions. One way to solve the Java to local operations is JNI. Java calls the local method through the JNI, and the local method is stored in the form of a library file (on the Windows platform is a DLL file form, which is the form of SO files on the UNIX machine). By calling the internal method of the local library file, Java can implement close contact with the local machine, call the interface method of the system level. Brief introduction and application is as follows: 1. Work you need to do in Java In the Java program, you first need to declare the name of the library called in the class, as follows: static {system.loadLibrary ("Goodluck");} Here, the library The extension name can not be written, is DLL or SO, which is determined by the system. It also needs to make a local statement on the method to be called, the keyword is Native. And only need to declare without a specific implementation. As follows: Public Native Static Void Set (INT i); public native static int get (); then compile the Java program file, generate a Class, use the javah command, JNI will generate a header file of C / C . For example, the 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 ());}} Compiles it with Javac Testdll.java to generate TestDLL.CLASS. Use Javah TestDLL, you will generate a TestDLL.h file in the current directory, which needs to be called to generate the required library files by the C / C program. Second, the work you need to do in C / C is required for generated .h header file, C / C , is to implement it. Then compile the connection to the library file. By copying the library file to the path to the Java program, you can call the functionality implemented by C / C with Java. Connect an example.