Welcome to click “Beauty of Algorithms and Programming” ↑ pay attention to us!

This article was first published on wechat public account: “Beauty of Algorithms and Programming”, welcome to follow.

1 why write this article

There are already many tutorials on the Internet that introduce JNI, so why bother writing this article?

I believe that we have encountered such a situation when reading some tutorial articles at ordinary times, according to the steps described in the tutorial to operate step by step, but the results did not get the expected results of the tutorial. The reason for giving up is very simple. For an unknown field, readers cannot solve the problem by themselves. However, the tutorial articles they read do not give a detailed description of the problem, so they finally choose to give up.

Through the analysis, we found that most of the tutorial articles have a common problem, that is, emphasis on steps and neglect the analysis of the problems that readers may encounter in the process of reading.

We hope to change this situation. For the tutorial articles, we will not only introduce the detailed steps, but also introduce the key problems that readers may encounter while doing this, and analyze these problems in detail to help you solve the problem thoroughly.

2 HelloNative tutorial

The following will introduce the preparation of JNI introductory tutorial HelloNative program.

The main steps are as follows:

1) Write HelloNative. Java program;

2) Compile and get the HelloNative. H header file;

3) Write HelloNative. C program;

4) Compile libHelloNative. Jnilib;

5) Run HelloNative.

To get a general idea of what we need to do, I’ll go through each step on the MAC and highlight the difficulties.

2.1 Write HelloNative. Java program

public class HelloNative{

    static{

        System.loadLibrary("HelloNative");  / / a difficult

    }

    public static native void sayHello(a);

    public static void main(String[]args){

        newHelloNative().sayHello(); }}Copy the code

2.2 Compile and get the HelloNative. H header file

Run the following command:

javac HelloNative.java

javah HelloNative
Copy the code

2.3 Write HelloNative. C program

#include <stdio.h>

#include "HelloNative.h"

JNIEXPORT void JNICALL Java_HelloNative_sayHello(JNIEnv *env, jclass jc) / / difficulties

{

    printf("Hello Native\n");

}
Copy the code

2.4 Compile libHelloNative. Jnilib

/ / difficult point three

gcc HelloNative.c -o libHelloNative.jnilib -dynamiclib - I/Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include / - I/Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include/DarwinCopy the code

2. 5 Run HelloNative

java HelloNative
Copy the code

3 The difficulties in analysis

Although the above introduction is the MAC system under the preparation of procedures, the reader’s system may be mostly Windows, but does not affect the preparation of everyone.

Next, I’ll look at the three difficulties with marking in Section 2, which are the most common ones.

The difficulties in the

JNI exists to make it easy for Java programs to interact with other programming languages like C/C++, and we can do that with JNI.

Some of you might ask why? Wouldn’t it be better for me to do all my tasks in Java? Why not use Java and C/C++ instead of multiple languages?

There are several reasons for this problem. Here are some reasons:

– The Java language runs on top of the JVM and is therefore highly dependent on the JVM. As we all know, this mechanism makes the Java language inefficient compared to other C languages, so some tasks requiring high performance can be written in C, and then the above programs can call the C-written modules through JNI.

– If your project team is a multilingual team with people from Java, Ruby, Python and other programming languages, how do you make the programs written by these people relevant?

So how does Java do this?

Compile a module written in another language into a dynamic library, and then load the dynamic library in a Java program to use the library.

System.loadLibrary(“HelloNative”); / / difficult point one

So what you see here is HelloNative, the dynamic library that the Java program loaded and compiled. HelloNative is not the full name of the final dynamic library. The name of the dynamic library is different for different operating systems.

Under Windows is called *. DLL

Under Linux call *. So

Mac is called *. Jnilib

We all know that Java is a cross-platform program, so it is definitely not possible to specify the full name of a platform-specific dynamic library in Java code, so it is only the name of the dynamic library rather than the full name.

The full name of the dynamic library we eventually generated on different platforms for this example is:

Windows             HelloNative.dll

Linux               libHelloNative.so

Mac                 libHelloNative.jnilib
Copy the code

This knowledge point is very useful, difficult 3 will be covered again, please be sure to master in advance.

The difficulties in the second

The definition of this function may be written incorrectly, and it is not always possible to follow the instructions.

If you’ve written c/ C ++ programs, you know that in C/C ++, a program is divided into a header file called hello.h and an implementation file called hello.c, where function declarations are defined in the header file and functions are implemented in the implementation file.

Now we know how to break through difficulty 2 in the HelloNative. C file.

Open the HelloNative. H file to find the corresponding function declaration.

JNIEXPORT void JNICALL Java_HelloNative_sayHello  (JNIEnv *, jclass);

Function declarations should be understandable. In this example, you should note that the declaration of a parameter does not specify the name of the parameter, but only the type of the parameter. So in this example, JNIEnv* and jclass are both types of parameters.

So the code you see in difficulty two looks like this, where env and jc are concrete parameter names.

JNIEXPORTvoid JNICALL Java_HelloNative_sayHello(JNIEnv *env, jclass jc

The difficulties in three

This difficulty is also where most of the problems are encountered, which needs to be emphasized.

How to compile a C language dynamic library?

We all know that a very famous compiler of C language is called GCC, so this article describes how to use GCC to compile dynamic libraries. (GCC is only one of the c compilers. There are many other compilers, such as Microsoft.)

The commands used to compile dynamic libraries vary from operating system to operating system. For example:

Linux: gcc-shared HelloNative. C-o libHelloNative. So Mac: gcc-shared HelloNative.  gcc -dynamiclib HelloNative.c -o libHelloNative.jnilibCopy the code

In the first difficulty, we explained that the names of dynamic libraries are different in different systems, which is reflected in this section.

Second, we need to give the paths to the jni.h and jni_md.h header files in the GCC compilation dynamic library.

So the next step is to find the directories where these two header files are located, and you can find them using various file search tools such as the powerful search engine everything for Windows. It can be found quickly on Linux and Mac by using the following command:

Locate the jni. H > / Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include/jni. H locate jni_md. H > / Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include/Darwin jni_md. HCopy the code

Note that we only need two directories for the files:

/ Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include /

/ Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include/Darwin

With the preparation done, we finally add the path to the header file and we’re done.

Mac:

gcc -dynamiclib HelloNative.c -o libHelloNative.jnilib - I/Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include / - I/Library/Java/JavaVirtualMachines jdk1.8.0 _131. JDK/Contents/Home/include/DarwinCopy the code

Note:

  • The I in -i is uppercase.
  • -i /Library There is no space between I and /Library.

Here we only show the path for MAC, similar for other systems.

4 conclusion

This paper introduces the JNI introductory tutorial HelloNative program, compared with other tutorials, we pay more attention to some difficulties in the actual learning process, through the analysis of the three difficulties, I believe you can better grasp the relevant knowledge points.

If you are interested in this kind of tutorial article, please continue to follow the “Beauty of Algorithms and Programming” wechat official account to learn more about this kind of article.