The role of the native

In summary: native is developed in conjunction with C++! The use of the native keyword indicates that the method is a native function, which means that the method is implemented in C/C++, compiled into a DLL, and called by Java.

  1. Native is used when Java works with other languages (such as c++), i.e. the implementation of a function native is not written in Java.
  2. Since it’s not Java, forget about the source code, we just need to know that this method is implemented.
  3. Native means to tell the operating system, this is a function that you have to implement for me because I’m going to use it. So native keyword functions are implemented by the operating system, Java can only call.
  4. Java is a cross-platform language. Since it is cross-platform, the cost is to sacrifice some control of the bottom layer. To realize the control of the bottom layer, Java needs the help of some other languages, which is the role of Native

JNI profile

Native methods are implemented through JNI in Java. JNI stands for Java Native Interface. Since Java 1.1, the Java Native Interface (JNI) standard has become part of the Java platform, allowing Java code to interact with code written in other languages.

JNI was originally designed for native compiled languages, especially C and C++, but it doesn’t prevent you from using other languages, as long as the calling convention is supported.

At present, there are three main technologies for Java to interact with DLLS: JNI, Jawin and Jacob.

In terms of current functionality: JNI >> Jawin > Jacob, its general structure is shown as follows:

Windows, based on native PE architecture, Windows JVM is based on Native architecture, Java application architecture is built on JVM. The JVM indirectly calls the target native function by loading this JNI program.

JNI generation steps – Mac version

  1. Write Java classes with native declared methods, generating.java files
  2. Compile the Java classes you wrote using the Javac command, generating.class files
  3. Generate a header file with the extension H using the Javah-jni Java class name, that is, generate a.h file
  4. Use C/C++ (or some other programming language) to implement native methods that create.h file implementations, that is, create.cpp files to implement methods in.h files
  5. Jnilib file is generated by generating dynamic link library from C/C++ files

JNI instance

Next we follow the steps above to generate JNI instances one by one

1. Write a Java class with native declaration, HelloWorld.java

public class HelloWorld { public native void sayHelloWorld(); // Declare a native method static{system.loadLibrary ()"HelloWorldImpl"); // Load the dynamic link library,"HelloWorldImpl"} public static void main(String[] args){HelloWorld HelloWorld = new HelloWorld(); helloWorld.sayHelloWorld(); }}Copy the code

2. Use Javac to generate helloWorld.class

javac HelloWorld.java
Copy the code

3. Use the Javah-jni Java class to generate a header file with the extension H

javah -jni HelloWorld
Copy the code

The important thing to note here is that we should execute at the root of the package name, because the file name of the generated file needs to reference the package directory. Otherwise, an error will be reported that the class file was not found

Generate the com_think_jni_helloWorld.h file

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_think_jni_HelloWorld */

#ifndef _Included_com_think_jni_HelloWorld
#define _Included_com_think_jni_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_think_jni_HelloWorld
 * Method:    displayHelloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_think_jni_HelloWorld_displayHelloWorld
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Copy the code

This h file is our interface in Java, where we declare a Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); Method, and then implement it in our native methods, which means that we must write C/C++ programs with the same method name as here

Implement native methods using C/C++

Create helloWorldimpl.cpp as follows:

#include "jni.h"
#include "com_think_jni_HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_com_think_jni_HelloWorld_displayHelloWorld(JNIEnv *env,jobject obj){
    printf("Hello World! \n");
    return;
}
Copy the code

5 Generate a dynamic link library from the files written in the local method

GCC - I/Library/Java/JavaVirtualMachines / [set] according to install the JDK version of/Contents/Home/include / - dynamiclib HelloWorldImpl. CPP - o libhell.jnilibCopy the code

However, an error occurs when running this command

file not found

We found this file in

/ Library/Java/JavaVirtualMachines / [set] according to install the JDK version/Contents/Home/include/DarwinCopy the code

So let’s link that directory to the command above

GCC - I/Library/Java/JavaVirtualMachines jdk1.8.0 _71. JDK/Contents/Home/include / - I/Library/Java/JavaVirtualMachines jdk1.8.0 _71. JDK/Contents/Home/include/Darwin / - dynamiclib HelloWorldImpl. CPP - o libhell.jnilibCopy the code

Libhel.jnilib was successfully generated

6 Run the executable file

After putting the above files in the same folder, execute

java HelloWorld
Copy the code

You can see


Handling the original address: www.jianshu.com/p/1ba925157…