JNI is the abbreviation for Java Native interface, and Chinese is a native call to Java. Using JNI makes it easy to use our Java program to call a C + + program. Most of the time, some functions are not implemented in Java, for example, some of the functions of the underlying driver, we can use JNI to invoke C or C + + program to implement, this is the power of JNI. But JNI also has its drawbacks, and using Java to interact with locally compiled code often loses platform portability.
Here is a jni example that calls C + + output "Hello World":
First step: Create a Java class, where you define a local method (using the Native keyword modification method)
public native void SayHello ();
Step two: Generate a C + + header file for the local method using the Javah command (full path to the Javah class)
In the DOS window, enter the directory where the project is located, then execute the javah com.test.TestNative command, and after execution, a header file with a suffix of. h is generated in the current directory, such as Com_test_ TestNative.h, this header file is named according to the package name and class name.
1/* Do not EDIT This file-it are machine generated */2 #include <jni.h> 3 * * Header for class com_test_testnative */4 5 #ifndef _included_com_test_testnative 6 #define _included_com_test_testnative 7 #ifdef __cplusplus 8 extern "C "{9 #endif10/*11 * Class: com_test_testnative12 * Method: sayHello13 * Signature: () V14 * * jniexport void Jnicall java_com_test_testnative_sayhello16 (jnienv *, jobject); #ifdef __CPLUSPLUS19}20 #end IF21 #endif
15, 16 lines are declarations of the Local method SayHello () in the Testnative class. This h file is equivalent to our interface in Java, which declares a Java_com_test_testnative_sayhello (jnienv *, Jobject) method, and then implements this method in our native method, In other words, we must use the same method name as the one used to write C + + programs.
Step three: Write the C + + native code to generate the dynamic link library file
Start by creating a DLL project in VC6.0 (or other tools, of course)---Win32 dynamic-link Library project. Then add the generated header file com_test_testnative.h to the project, and then create a source file that references the header file and implements the functionality of the local function in the header file:
1 #include <iostream.h>2 #include "com_test_testnative.h" 3 4 jniexport void Jnicall java_com_test_ Testnative_sayhello (jnienv *env, Jobject obj) 5 {6 cout<< "Hello world!" <<endl;7 }
This is due to the introduction of jni.h in Com_test_testnative.h, so jni.h is added to the Include directory under the VC6.0 installation directory. Jni.h in the include in the JDK installation directory, the two header files in Include/win32 jawt_md.h, jni_md.h are also imported into VC6.0.
After importing the dependent header file, we can build the project, press F7 on the line, it will be in the project directory in the DEGUG directory to generate a dynamic link library file, I generated a NativeCode.dll. We can copy the DLL file to the environment variable in the directory contained in the directories to our Java program call, for convenience, we can also add the project directory where the DLL is in the environment variable path, so as to avoid the trouble of copying every time. Note Restart MyEclipse After you modify the environment variable.
Fourth step: Java calls local functions
1 package com.test; 2 3 public class Testnative {4 public native void SayHello (); 5 6 /** 7 * @param args 8 */9 public static void Main (string[] args) { system.loadlibrary ("Nativecode"); testnative tnative = new Testnative (); Tnative.sayhello (); }14}
The 10th line is to load the dynamic link library, the JVM only needs to be loaded once can be called, "Nativecode" is the name of the dynamic link library generated above, without the suffix name.
Run the program and print out "Hello World" successfully.