Spring Boot projects use the Java language, and Java cannot call go directly; An indirect way to do this is to compile the GO code into a dynamically linked library, a DLL file for Windows and a SO file for Linux. Use Java local call to achieve, commonly used Java native JNI, and Oracle tripartite library JNA, this article to JNA as an example.

process

Refer to the dynamic library generated by Java calls to Golang (DLL,so)

Pay attention to

The above blog post only covers Java calls, but spring calls are different from Java calls, and we expect the (DLL,so) files to still be available after being packaged into jars with the project. After practice, found that there are two main problems, one is the file path problem (where to put), the other is the operating system environment problem, let’s solve one by one. Let’s say we program dynamic linked libraries named testsdK.dll and testsdK.so. The Java interface is roughly as follows:

import com.sun.jna.Library;
import com.sun.jna.Native;

public interface InnoEncryptSdkLib extends Library {

    InnoEncryptSdkLib INSTANCE = Native.load("testSdk", InnoEncryptSdkLib.class); // Interface method... }Copy the code

Note that the above resource name does not have an extension (i.e., a suffix), just write the file name testSdk.

1. The file path is faulty

Environment: resources\linux-x86-64\ libtestsdk.so

Windows environment: Resources \win32-x86-64\ testsdK.dll

Create two new folders, linux-x86-64 and win32-x86-64, testsdk. DLL under win32-x86-64, testsdk. so under linux-x86-64. For those of you who have an eye for Linux, you may have noticed that you have put libtestsdk. so instead of testsdk. so in linux-x86-64. Which brings us to our second question:

2. The operating system environment is faulty

In Linux, if the file name (including the suffix) is not specified in the jNA call, the system is looking for the dynamic library named libtestsdk. so, not the testsdK. so file. The testsdK. so file will be renamed as libtestsdK. so and placed in the specified location.

Ok, so we can use our compiled dynamic library with confidence.