A. What is native method
simply put, a native method is an interface that Java invokes non-Java code. A native method is a Java approach: the implementation of this method is implemented by a non-Java language, such as C. This feature is not unique to Java, many other programming languages have this mechanism, such as in C + +, you can use extern "C" to tell the C + + compiler to call a C function.
"A native method are a Java method whose implementation is provided by Non-java code."
When defining a native method, it does not provide the implementation body (some like the definition of a Java interface), because its implementation body is implemented by non-Java languages outside. , an example is given below:
Package Java.lang;
public class Object {
...
.. Public final native class<?> GetClass ();
public native int hashcode ();
Protected native Object clone () throws Clonenotsupportedexception;
Public final native void Notify ();
Public final native void Notifyall ();
Public final native void wait (long timeout) throws interruptedexception;
......
}
The identifier native can be used with all other Java identifiers, except for abstract. This is reasonable because native implies that these methods are implemented, except that these implementations are not Java, but abstract clearly indicates that these methods are not implemented. When native is fonts with other Java identities, its meaning is no different from that of non-native method.
A native method can return any Java type, including a non basic type, and can also have exception control. The implementation body of these methods can make an exception and throw it out, which is very similar to the Java approach.
The presence of native method does not have any effect on other classes invoking these local methods, and other classes that actually call these methods do not even know that it is calling a local method. The JVM will control all the details of invoking the local method.
If a class that contains a local method is inherited, the subclass inherits the local method and can override the method in the Java language (this may seem odd), and the same if a local method is fianl, it cannot be overridden after it is inherited.
Local methods are very useful, Because it effectively expands the JVM. In fact, the Java code we write has been used locally, and in the implementation of the Sun's Java Concurrency (multithreading) mechanism, many contact points with the operating system have been used locally, allowing Java programs to transcend the Java runtime boundaries. With local methods, Java programs can do any application-level tasks.
Ii. Methods of Use
The native keyword illustrates that the method of modification is an original ecological 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), a native programming interface that is part of the Java Software Development Toolbox (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) in a native application, allowing programmers to invoke Java code from within native code.
However, calls outside of Java are typically not ported to other platforms, and security exceptions can also be thrown in the applet. Implementing native code will not allow your Java application to pass 100% pure Java tests. However, if you must perform a local call, consider several guidelines:
1. Encapsulate all your local methods into a class that invokes a single DLL. For each of the target operating system platforms, you can use DLLs that are specific to the appropriate platform version. This minimizes the impact of local code and helps to take into account the migration issues that are required 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. Make your local methods as independent as possible to minimize the overhead required to load your DLLs and applications. If a run-time DLL is required, it must be provided with the application.
The writing steps for JNI are as follows:
- A. Writing Java classes with a method of native declarations
- B. Compiling Java classes with the Javac command
- C. Use JAVA-JNI * * * to generate a header file with a suffix named. h
- D. Implementing local methods using other languages (c, C + +)
- E. Generating a dynamic link library from a file written by a local method
The following is a simple example of invoking a local C program in Java:
A. Preparation of 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. Generate. h files
Javah-jni HelloWorld
The generated content is as follows:
/* Do isn't EDIT this file-it is 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 that is used when invoking the Jni method. The second parameter is a handle to the Java object HelloWorld that is 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 implementation
Here, for example in Windows, you need to generate a DLL file. Under Save the HELLOWORLDIMPL.C folder, use the VC compiler CL.
Cl-i%java_home%\include-i%java_home%\include\win32-ld Helloworldimp.c-fehello.dll
Note: The generated DLL filename is configured after the option-fe, which is Hello, because the name we loadlibary in the Helloworld.java file is hello. Of course, there are changes to be done here. Additional-i%java_home%\include-i%java_home%\include\win32 parameters need to be added because the jni.h file was introduced when the local method was written in step fourth.
F. Running the program
The Java HelloWorld is OK!
The above is about Java in the native keyword detailed introduction, hope for everyone's learning help.