JNI overview JNI is the abbreviation of Java Native Interface ". JNI technology is usually used to achieve the following two points: (1) functions in Java programs can call Native language functions. Native functions generally refer to functions written in C/C ++; (2) functions in Native programs can call Java-layer functions, that is, Java-layer functions can be called in C/C ++ programs. Of course, everything has two sides, and JNI is no exception. The main disadvantages of using JNI are as follows: (1) Using JNI technology will make Java Application not cross-platform. If Native code is to be transplanted to other platforms, Native code must be rewritten. (2) Java is a strongly typed language, and C/C ++ is not, therefore, be cautious when writing JNI. JNI -- Hello World when learning any language technology, we should first write a simple "Hello World". Next we will introduce how to use JNI technology to implement Hello World. (1) first declare a native method in the Java class and create a NativeDemo project in eclipse. The class name is TestNative. The specific code is as follows: [java] package com. jony. nativetest; public class TestNative {public native void sayHello ();/*** @ param args */www.2cto.com public static void main (String [] args) {// TODO Auto-generated method stub} (2) use the Javah command to generate the C/C ++ header file containing the native method definition; in the Java installation path (D: \ Java \ jdk1.6.0 _ 24 \ bin) You can see a lot of executable files, including common java, javac, javah and other commands. First, let's take a brief look at the usage of the javah command. In the command line, enter javah-help to view the command parameters and usage. Next, we will compile TestNative. the java file is compiled into a class file. In my local code, the corresponding path is: E: \ Workspaces \ android \ NativeDemo \ bin. For details, enter: cd E: In the command prompt box: \ Workspaces \ android \ NativeDemo \ bin and E: Enter the corresponding file directory, and enter javah com in the command prompt box. jony. nativetest. testNative command generation. h header file, as shown in: After executing the preceding command, you can view the generated file in path E: \ Workspaces \ android \ NativeDemo \ bin. h header file. The generated code is as follows (com_jony_nativetest_TestNative.h): [cpp]/* do not edit this file-it is machine generated */# include <jni. h>/* Header for class com_jony_nativetest_TestNative */# ifndef _ classes # define _ classes # ifdef _ cplusplus extern "C" {# endif/** Class: Classes * Method: sayHello * Signature: () V */JNIEXPORT Void JNICALL Java_com_jony_nativetest_TestNative_sayHello (JNIEnv *, jobject); # ifdef _ cplusplus} # endif (3) write the C/C ++ source file according to the generated C/C ++ header file. First, create a Win32 Dynamic-Link Library project named NativeCode02 in the VC ++ compiler, copy the previously generated header file (com_jony_nativetest_TestNative.h) to the root directory of the NativeCode02 project. Next, create the source. cpp file. The directory structure: C ++ code is relatively simple, as shown in the code. (4) Compile the C/C ++ source file into a dynamic link library (DLL-executable binary code). When compiling the source file (Ctrl + F5), the following exception occurs: -------------------- Configuration: NativeCode02-Win32 Debug -------------------- Compiling... source. cppi: \ archive_software \ c & c ++ \ vc6.0 complete green version \ myprojects \ nativecode02 \ com_jony_nativetest_testnative.h (2): fatal error C1083: Cannot open include file: 'jni. h': No such file or directory error when cl.exe is executed. nativeCode02.dll-1 error (s), 0 warning (s)) Indicates that the jni. h header file is not found during compilation. This header file is provided in the Java JDK, so we can find this header file in the Java installation path (D: \ Java \ jdk1.6.0 _ 24 \ include, copy it to the Header Files directory of the NativeCode01 Project (Note: Because jni. h. The header file has been copied to the local device. Therefore, replace <> with "") with ") for re-compilation (Crtl + F5). The following exception occurs: ------------------ Configuration: nativeCode02-Win32 Debug -------------------- Compiling... source. cppi: \ archive_software \ c & c ++ \ vc6.0 complete green version \ myprojects \ nativecode02 \ jni. h (27): fatal error C1083: Cannot open include file: 'jni _ md. H ': No such file or directory error when cl.exe is executed. nativeCode02.dll-1 error (s), 0 warning (s) this exception is also very easy to understand, lack the jni_md.h header file, also in the Java installation path () find the Header file and copy it to the Header Files directory. then compile: -------------------- Configuration: NativeCode02-Win32 Debug -------------------- Compiling... source. cppLinking... creating library Debug/NativeCode02.lib and object Debug/NativeCode02.expNativeCode02. dll-0 error (s), 0 warning (s) OK, compiled successfully, in the project The NativeCode02.dll file compiled successfully is displayed in the Debug directory of the project. (5) Save the dynamic link library path to the path, so that you can directly call the dynamic link library (DLL) in Java code. The specific steps are omitted (6) compile the Java-Layer Code. After the dynamic link library is compiled successfully, you can call the dynamic link library at the Java layer. The code is simple and you can directly run the code: [java] package com. jony. nativetest; public class TestNative {public native void sayHello ();/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. loadLibrary ("NativeCode02"); // load the dynamic link library TestNative testNative = new TestNative (); testNativ E. sayHello (); // call the naive method} OK. The success is that JNI -- Hello World has been implemented so far. The following blog will introduce you in depth how to use JNI. The technology comes from sharing, common progress ......