Recently read Java source code, found a lot of methods with native decoration, then what is the native method?
In a nutshell, a native method is a Java interface that calls non-Java code. A native method is a Java approach: the implementation of this method is implemented by non-Java languages, such as C. This feature is not specific to Java, many other programming languages have this mechanism, such as in C + +, you can use the extern "C" to tell the C + + compiler to call a C function.
Jni is J . the abbreviation for Ava Native interface. Beginning with Java 1.1, the Java Native Interface (JNI) standard became part of the Java platform, allowing Java code to interact with code written in other languages. JNI was initially designed for local compiled languages, especially C and C + +, but it does not prevent you from using other languages, as long as the calling convention is supported.
Using Java to interact with locally compiled code often loses platform portability. However, in some cases this is acceptable and even necessary, such as using some old libraries, interacting with the hardware, the operating system, or to improve the performance of the program . The JNI standard guarantees at least local code to work with any Java virtual machine implementation.
Writing steps for JNI (Java Native Interface):
1. Write Java class with native declaration method
2. Compile the Java class written using the Javac command
3. Use Javah. JNI Java class name to generate a header file with the extension h
4. Implement the Local method in C + + (or other programming language)
5. Generate a dynamic connection library for files written in C + +
Here's a small example of how Java calls the methods in C via JNI:
1. Write the Java code as follows:
Package Com.xiaomo.jni;public class Jni_test {public native void Showstr (String name);p ublic native void add (int a,int b); public native int combinestr (int a,int b); Static{system.loadlibrary ("Jni_dll");} public static void Main (string[] args) throws Interruptedexception {jni_test JNI = new Jni_test (); Jni.showstr ("hello!"); Jni.add (1, 2); Thread.Sleep (5000); System.out.println (Jni.combinestr (5, 2));}}
2. Compile into a . class file, remember to configure environment variables Oh, will not configure the children's shoes to supplement their own extracurricular knowledge bar ╮(╯▽╰)╭.
3. Generate the . h file:javah –jni com.xiaomo.jni.JNI_Test will be found in Jni_test's package directory Com_xiaomo_jni_jni_ Test.h file
Note: Classes with the package are returned to the root of the bundle to invoke the Javah command, or the class file will not be found! (Fall over ╮(╯▽╰)╭ here)
4.. DLL file generation
1) Open Visual C + + 6.0, select File - new project ( Select Win32 Dynamic-link Library ) - Determine - blank. DLL Engineering - completed.
2) Select Tools , options , directory ( Add directory under JDK installation directory INCLUDE and include\win32 two directories).
com_xiaomo_jni_jni_test.h Copy to the new the project folder.
3) Add the source file jni_test.cppand complete the local method you want to implement:
/* Don't EDIT this file-it are machine generated */#include <jni.h> #include "com_xiaomo_jni_jni_test.h" #include &l t;stdio.h>/* * Class: com_xiaomo_jni_jni_test * Method: showstr * Signature: (ljava/lang/string;) V * * jniexport void Jnicall java_com_xiaomo_jni_jni_1test_showstr (jnienv * env, jobject obj, jstring str) {printf ("%s\n" , &STR);} /* Class: com_xiaomo_jni_jni_test * Method: Add * Signature: (II) V */jniexport void Jnicall Java_com_xiaomo_jni _jni_1test_add (JNIENV * env, Jobject obj, jint A, jint b) {int sum = a + b;printf ("%d", sum);} Jniexport jint jnicall java_com_xiaomo_jni_jni_1test_combinestr (jnienv * env, Jobject obj, jint A, jint b) {int sum = A + B;return sum;}
5. This generates a jni_test.dll file under the C + + Project (under Project debug folder)
6. Copy the jni_test. dll file into the Bin folder under the JDK installation directory and run java com.xiaomo.jni.JNI_Test console printing (garbled problem here will not say, I slowly study it ╮(╯▽╰)╭):
The source of the fall: http://blog.csdn.net/cl05300629
Java calls a small demo of C via JNI