Package path: java.lang.*:
Java.lang.Object:
The object class is the root of the Java class tree and the superclass of all classes.
Ii. Methods and Members:
Private Static native void registernatives (); Static {registernatives ();}
Registernatives () is the first method of the object class, and the following static code block is called immediately after the Registernatives ().
1. Thenative keyword indicates that its modification is a primitive method, and the corresponding implementation of the method is not in the current file, but in files 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 a Java Software Development toolkit (Java Software Development Kit, SDK) is part of the. 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.
Native usage :
1. Writing Java classes (Java files) with the method of the native declaration
2. Java classes (class files) compiled with the Javac command
3. Use JAVAH-JNI * * * * to generate a header file (. h file) with a suffix of. h
4. Implementing local methods using other languages (c, C + +)
5. Generate a dynamic-link library (DLL file) for files written by local methods
Cases:
1. Write code (NATIVEDEMO.JAVA):
class nativedemo{ publicnativevoid info (String info); Static { system.loadlibrary ("info"); } Public Static void Main (string[] args) { new Nativedemo (). info ("JNI"); } }
2.javac Command Compilation
Javac Nativedemo.java
3. Generate the. h file (Javah Nativedemo not followed by the. class suffix)
Javah-jni Nativedemo
Generated file (NativeDemo.h) content:
/* do not EDIT this file-it are machine generated */ #include <jni.h>/* Header for class Nativedemo */ #ifndef _included_nativedemo#define _included_nativedemo#ifdef __ Cplusplusextern "C" {#endif /* Span style= "COLOR: #008000" > * Class:nativedemo * Method:hello * Signature: (ljava/lang/string;) V */ jniexport void Jnicall Java_nativedemo_hello (jnienv *
4. Implement local method (implement NATIVEDEMOIMP.C in C language)
#include <jni.h>#include"NativeDemo.h"#include<stdio.h>void Jnicall java_nativedemo_info (jnienv *env,jobject obj,jstring name) { constChar * STR; = (*env),getstringutfchars (env,name,null); if (str = = NULL) {return; } printf ("Hi,%s This is the C implementation!\n", str); return ;}
5. Generate a dynamic link (compile Nativedemoimp.c as a. dll file)
Way One:
Cl-i%java_home%\include-i%java_home%\include\win32-ld helloworldimp.c-Fe Info.dll
-m64 -wl,--add-stdcall-alias-i "C:\Program files\java\jdk1.8.0_131\include"-i "C:\Program files\java\jdk1.8.0_ 131\include\win32 "-shared-o Hello.dll hello.c
Note: The generated DLL file name is configured after the option-fe, this is info because the name we used in the Nativedemo.java file is info. loadlibary. 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.
Java source notes (1) native analysis of Java.lang.Object