Originally from: http://blog.csdn.net/funneies/article/details/8949660
The native keyword indicates that its modification is a primitive method, and the corresponding implementation of the method is not in the current file, but in a file implemented in other languages, such as C and C + +. The Java language itself cannot access and manipulate the underlying operating system, but other languages can be invoked through the JNI interface to enable access to the underlying.
JNI is a Java Native interface (Java Native Interface) and is a native programming interface that is part of the Java Software Development Toolkit (Java Software Development kit,sdk). JNI allows Java code to use code and code libraries written in other languages. The invocation API (part of the JNI) can be used to embed a Java Virtual machine (JVM) into a native application, allowing programmers to invoke Java code from within native code.
However, calls outside of Java are often not ported to other platforms, and security exceptions can be thrown in applets. Implementing native code will make your Java application fail to pass 100% pure Java testing. However, there are several guidelines to consider if you must perform a local call:
1. Encapsulate all your local methods into a class that calls a single DLL. For each target operating system platform, you can use a DLL that is specific to the appropriate platform version. This minimizes the impact of native code and helps to take into account the migration issues that are needed later.
2. The local method is as simple as possible. Try to minimize the reliance of your local methods on third-party (including Microsoft) run-time DLLs. Keep your local methods as independent as possible to minimize the overhead required to load your DLLs and applications. If a runtime DLL is required, it must be supplied with the application.
The following are the steps to write JNI:
A. Writing a Java class with a native declaration method
B. Compiling a Java class using the Javac command
C. Use JAVA-JNI * * * * to generate a header file with a suffix of. h
D. Implementing local methods using other languages (c, C + +)
E. Generating a dynamic-link library of files written by local methods
The following is a simple example of calling a local C program in Java:
A. Writing the Helloworld.java class
Class helloworld{
public native void Hello ();
static{
System.loadlibrary ("Hello");
}
public static void Main (string[] args) {
New HelloWorld (). hello ();
}
}
B. Compiling
Javac Helloworld.java
C. Generating the. h file
Javah-jni HelloWorld
The generated content is as follows:
/* Don't EDIT this file-it are machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _included_helloworld
#define _included_helloworld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:helloworld
* Method:hello
* Signature: () V
*/
jniexport void Jnicall Java_helloworld_hello
(JNIENV *, jobject);
#ifdef __cplusplus
}
#endif
#endif
The first parameter is the JNI environment pointer used when invoking the Jni method. The second parameter is a handle to the Java object HelloWorld instantiated in this Java code. Other parameters are parameters of the method itself
D.C implementation
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
Jniexport void Jnicall Java_helloworld_hello (jnienv *env,jobject obj) {
printf ("Hello world!\n");
Return
}
The first line is the introduction of the Jni.h file (in the%java_home%\include directory) with the definition of jnienv and jobject.
E. Compiling C implementations
Here, for example in Windows, you need to generate a DLL file. Under Save Helloworldimpl.c folder, use VC's compiler to CL.
Cl-i%java_home%\include-i%java_home%\include\win32-ld Helloworldimp.c-fehello.dll
Note: The generated DLL file name is configured after the option-fe, here is hello, because in the Helloworld.java file we loadlibary used the name is hello. Of course there is a need to modify it after this change. It is also necessary to add the-i%java_home%\include-i%java_home%\include\win32 parameter because the jni.h file is introduced when writing the local method in step fourth.
6) Running the program
The Java HelloWorld is OK!
Examples are done according to other people's Web pages, there are many other things, you can see the resource connection.
Resources:
An article from the IBM website: http://www.ibm.com/developerworks/cn/java/jnimthds/
China itpub article: http://java.chinaitlab.com/JDK/36677.html
Http://java.chinaitlab.com/JDK/36678.html
J2SE5 Java Native Interface specification
Http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html
Usage of native in Java