简体   繁体   中英

Linux & Java: no native in java library path whereas it is there

I use JNI in my Java code to communicate with C++. The C++ code is compiled and stored in a native library that can be accessed by Java. On Windows, I use IntelliJ to run my code and added the folder that contains the native library to my -Djava.library.path. No issues here, my code runs just fine and is able to locate and work with the library.

My next step is to have it all working on a server with Linux. Here, I run into problems. Using GitHub, I pulled my code on the server, created new objects of my.cpp files and compiled it into a native.so library. Next, I call my experiment using

java -Djava.library.path=/home/usr/ILOG/CPLEX_Studio1210/cplex/bin/x86-64_linux:/home/usr/FSVRPpd/lib -cp ./target/FSVRPpd-1.0.jar -Xmx15g fvrpsd.test.FSVRPpdTest

The library path includes references to two folders, one for CPLEX and the other one to the folder containing the native library.

In my Java code I have:

 static {System.out.println(System.getProperty("java.library.path")); System.loadLibrary("native");}

to print the java library path (as a check) and load the library. Running my code returns the following output:

/home/usr/ILOG/CPLEX_Studio1210/cplex/bin/x86-64_linux:/home/usr/FSVRPpd/lib
Exception in thread "main" java.lang.UnsatisfiedLinkError: no native in java.library.path: 
[/home/usr/ILOG/CPLEX_Studio1210/cplex/bin/x86-64_linux, /home/usr/FSVRPpd/lib]
    at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2660)
    at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:829)
    at java.base/java.lang.System.loadLibrary(System.java:1867)
    at fvrpsd.test.FSVRPpdTest.<clinit>(FSVRPpdTest.java:136)

It shows that the locations are correctly included, but it unable to find the native library, even though it is located in the second folder.

I read many posts on this on this website and tried including adding the java library path to LD_LIBRARY_PATH , but without any success.

I am looking forward to any suggestions on what I can do to identify the issue.

Your native library must be called libnative.so on Linux and native.dll on Windows.

From the documentation of System.loadLibrary :

Loads the native library specified by the libname argument.
The libname argument must not contain any platform specific prefix, file extension or path.

This aids in writing cross-platform code because the pattern is different for every platform. You can calculate the expected file name yourself by using System.mapLibraryName , for example:

# Linux
jshell> System.mapLibraryName("native")
$1 ==> "libnative.so"

# macOS
jshell> System.mapLibraryName("native")
$1 ==> "libnative.dylib"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM