Abstract: most of the frameworks are developed in a single language. JNI, the powerful function provided in Java, is gradually forgotten.

This article is shared from the Huawei Cloud Community " Java, a powerful feature that is gradually forgotten, so powerful that you can't believe it! ! ", author: Binghe.

Before the advent of the Java language, many systems were developed using C and C++. After the emergence of Java, because its object-oriented thinking is more in line with people's thinking habits, Java does not require programmers to manually manage memory allocation and recovery like C and C++. To put it bluntly, it is simple and easy to use. Due to the many advantages of Java, it has leapt to the top of the list of programming languages for many years.

In order to be able to interact with programs written in C and C++, Java provides the characteristics of native methods, which is what we often call JNI technology. However, with the rapid development of the Internet, distributed, microservices, big data, cloud computing Such technologies and frameworks emerge in endlessly, but most frameworks are developed in a single language. JNI, the powerful function provided in Java, is gradually forgotten.

Why use JNI

Recently, we have analyzed more than 500 terabytes of data and analyzed users’ behavior habits from more than 500 terabytes of data in order to provide users with a better product experience and recommend products that are more suitable for users. However, in the process of realizing the algorithm, the algorithm developed using the Java language separately analyzes the behavior of a certain user for a certain period of time from more than 500 terabytes of data, which consumes a lot of time. No matter how I optimize the algorithm, I can't achieve the expected results. Obviously, this does not meet the performance requirements.

A small partner said to me: try the C language. Yup! Why don't I try to write the algorithm in C language, so I wrote the algorithm in C language, after continuous optimization and adjustment, it can be regarded as preliminary to the algorithm performance requirements. But when displaying data to the big data screen, the backend still needs to be deployed in the form of microservices, so I thought of the JNI technology in Java

How to use JNI

Let me talk about the pits when using JNI to avoid repeating the pits. Here, everyone needs to pay attention to: when using JNI technology to call dll dynamic link library, 32-bit dll can only be called by 32-bit JDK , 64-bit dll can only be called by 64-bit JDK. This must be like this. If it is found that it cannot be called or that the version is wrong, first check whether the bit number of the JDK and the bit number of the dll correspond.

In order to allow the friends to develop their own JNI programs smoothly according to the article, here, I will talk about how to develop a JNI program in detail, mainly in three major aspects to illustrate how to use JNI technology to call C and C++ written program.
image.png

Note: In this article, I use the jna Java class library to implement JNI development.

Develop dll dynamic link library

Download VS

Use VS to develop dll

VS new project
image.png

Enter project name
image.png

Select an empty project and click Finish
image.png
image.png
image.png

After the creation is complete, copy the following code into it:

#include <windows.h> 
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl; 
 
#define MYLIBAPI extern "C" __declspec( dllexport ) 
 
//这的参数是必须的,也可以定义为.c头文件
MYLIBAPI double add(double a,double b);
MYLIBAPI double mul(double a,double b);
MYLIBAPI char * getString(char* a);
 
double add(double a,double b){  
    return a + b;  
}
 
double mul(double a,double b){
    return a*b;
}
//定义了一个返回java String类型的参数
char * getString(char* a){
    char* b ="this is test";
    return strcat(a,b);
}

What should be noted here is: java String is different from cpp String, its corresponding is char, if you want to use cpp string, it is either garbled or the call fails. *

Use VS to generate dll

Here becomes Release, click on the configuration manager to configure the x64 version, so the generated dll is the x64 version, which is very important.
image.png
image.png

After the configuration is complete, right-click the project and click the Generate button.
image.png

After this operation, the dll can basically be generated correctly. If it cannot be generated, it is most likely that your posture is wrong. Follow the article and try again. If it still doesn't work, you can add me to WeChat and ask me.

Where is the dll file generated by VS? Don't worry, let's continue.

Right click on the project
image.png

should be noted here that it is in the superior directory! Don't take the location of the opened project for granted and just go to x64 to find it, it's useless at all! There is no dll in it, it is in the parent directory, the x64 location of the parent directory.
image.png
image.png

Develop Java programs

Import Maven dependencies

After creating a new Maven project, introduce the following dependencies in the Maven pom file.

<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.3.1</version>
</dependency>

Specify dll location

I personally put it under the lib package, so that when importing this package, I can write the absolute path or the relative path.
image.png

Write code

Note: The interface method name defined here needs to be consistent with the method name in the dll.

package com.binghe.jni;
 
import com.sun.jna.Library;
import com.sun.jna.Native;
 
/**
 * @author binghe
 * @description: 测试JNI程序
 */
public class JnaTest {
    public interface TestProject extends Library {
        TestProject INSTANCE = (TestProject) Native.load("src/main/lib/testDll.dll",
                JnaTest.TestProject.class);
        public double add(double i, double j);
        public double mul(double i, double j);
        public String getString(String a);
 
    }
 
    public static void main(String[] args) {
 
        System.out.println(TestProject.INSTANCE.add(20.11,20.0));
        System.out.println(TestProject.INSTANCE.mul(16.9,20.89));
        System.out.println(TestProject.INSTANCE.getString("我现在正在测试dllgihjb"));
    }
}

Run the Java program

Run the main method directly, and get the following output.
image.png

That's it~~

Click to follow, and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量