Moment For Technology

Power in Java, forgotten?

Posted on Dec. 2, 2022, 5:49 p.m. by 王怡萱
Category: The back-end Tag: The back-end java

Speaking of Java, simple and easy to use, but Java has a lot of very powerful technology is gradually forgotten ~~

Before the Java language came along, many systems were developed using C and C++. After the emergence of Java, because of its object-oriented thinking is more in line with people's thinking habits, Java does not need to programmer manual management of memory allocation and recycling as C and C++. To put it bluntly, it's simple and easy to use. Because of its many advantages, Java jumped to the top of the list of programming languages for many years.

In order to interact with programs written in C and C++, Java provides the feature of native methods, also known as JNI technology. However, with the rapid development of the Internet, distributed, microservices, big data, cloud computing and other technologies and frameworks emerge one after another, but most of the frameworks are developed in a single language. JNI, a powerful feature provided in Java, has been gradually forgotten.

Why use JNI

Recently, more than 500 TB of data has been analyzed to analyze users' behaviors and habits in order to provide users with better product experience and recommend more suitable products. However, in the process of implementing the algorithm, the algorithm developed in Java language alone analyzes the behavior of a user in a certain period of time from more than 500 TB of data, which consumes a great amount of time. No matter how to optimize the algorithm, can not achieve the desired effect. Obviously, this does not meet the performance requirements.

A little friend said to me: try C language. Yeah! Why don't I try to use C language to write the algorithm ah, so, using C language to write the algorithm, after continuous optimization and adjustment, is a preliminary algorithm performance requirements. But when it comes to presenting data to a large screen of data, the back end still has to be deployed as a microservice, so I thought of the JNI technology in Java

Note: I'll write a separate post on how I analyzed more than 500 terabytes of data.

How to use JNI

First talk about what pits when using JNI, in order to avoid small friends repeat step pit, here, we need to pay attention to is: when using JNI technology to call DLL dynamic link library, 32-bit DLL can only be called 32-bit JDK, 64-bit DLL can only be called 64-bit JDK. This must be so, if you find that the call cannot be called or a version error is displayed, first check whether the number of bits in the JDK and the number of bits in the DLL correspond.

In order to be able to let friends smoothly according to the article to develop their own JNI program, here, I will detail how to develop a JNI program, mainly divided into three big aspects to explain how to use JNI technology to call C and C++ written program.

Note: The JNA Java class library is used in this article to implement JNI development.

Develop DLL dynamic link library

Download the VS.

Friends can reply "VScode" in the [Glacier Technology] public account, get VS2010 download link.

Use VS to develop DLLS

VS New Projects

Enter a project name

Select the empty item and click Finish

Once created, copy the following code in:

#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); 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; } // Define an argument that returns Java String type char* getString(char* a){char* b ="this is test"; return strcat(a,b); }Copy the code

Note: Java String is not the same as CPP String, which is corresponding to char*. If you want to use CPP String, it is either garbled or the call fails.

Use VS to generate DLLS

I'm going to change this to Release, and I'm going to click on the configuration Manager to configure the X64 version, so that the DLL that's generated is the X64 version, which is very important.

Right click the project and click the Build button when the configuration is complete.

This one operation down, basically can generate DLL correctly, if can't generate, most likely is your posture is wrong, according to the article again, if still not, you add my wechat ask me.

Where is the DLL generated by VS? Don't worry, let's move on.

Right-click on the project

Note here is in the parent directory! Don't assume the open project location and go directly to x64 to find it, it doesn't work! There's no DLL in it, it's in the parent directory, the x64 location in the parent directory.

Developing Java programs

Import Maven dependencies

After creating a new Maven project, introduce the following dependencies into the Maven POM file.

! -- https://mvnrepository.com/artifact/net.java.dev.jna/jna -- dependency groupIdnet.java.dev.jna/groupId The  artifactId  jna  / artifactId   version  5.3.1  / version   / dependency  ! -- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -- dependency groupIdnet.java.dev.jna/groupId  artifactId  jna - platform  / artifactId   version  5.3.1  / version   / dependency Copy the code

Specify the DLL location

I personally put it under the lib package so that when importing the package, I can write either absolute or relative paths.

Write the code

Note: The interface method name defined here must be the same as the method name in the DLL.

package com.binghe.jni; import com.sun.jna.Library; import com.sun.jna.Native; /** * @author binghe * @description: Public class JnaTest {public interface 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. The out. Println (TestProject. The INSTANCE. The mul (16.9, 20.89)); System. The out. Println (TestProject. INSTANCE. Get string (" I'm now testing dllgihjb ")); }}Copy the code

Running a Java program

Run the main method directly and you get the following output.

And we're done

Reference: juejin. Cn/post / 698649...

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.