Download .so Linux file on Java Runtime

I am trying to download the linux.so file at runtime in Java, but I get UnsatisfiedLinkError. I pass the argument -Djava.library.path = / Users / tom / codebase / jni / dist VM when starting below the main java from my Test.class. The libSample.so file is located in the / Users / tom / codebase / jni / dist directory. Any ideas? Thanks!

public class Test { public static void main(String[] args) { System.out.println(System.getProperty("java.library.path")); //prints /Users/tom/codebase/jni/dist System.loadLibrary("Sample"); } } 

VM argument:

 -Djava.library.path=/Users/tom/codebase/jni/dist 

An exception:

 Exception in thread "main" java.lang.UnsatisfiedLinkError: no Sample in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1045) at Test.main(Test.java:9) 

I also tried using a direct approach attempt (using System.load) and got the results below if they help any Exception in the main thread java.lang.UnsatisfiedLinkError: /Users/tom/codebase/jni/dist/libSample.so: No matching image found. Found: /Users/tom/codebase/jni/dist/libCiscoEnergyWiseJni.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00

+4
source share
4 answers

Libraries on Linux are often called in the libXXX.so template, and I believe Java follows this convention. Therefore, System.loadLibrary("Sample") can search for libSample.so . You can verify this by running a quick test program to call System.mapLibraryName and verify the output.

To fix this problem, assuming that this is actually a problem, you can either rename the library file or use System.load (not System.loadLibrary ), which will load the library indicated by the exact file name. pass it without any conversion. The latter method is not portable across platforms.

+8
source

I ran into the same problem on Linux and decided to set the LD_LIBRARY_PATH variable

 export LD_LIBRARY_PATH=<Lib File Path>:$LD_LIBRARY_PATH. 

Hope this helps

0
source

Try using

 Runtime.getRuntime().load(resource); 
-1
source
 public class Demo { static { try { System.load("/home/libsofile.so"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } } 
-2
source

Source: https://habr.com/ru/post/1380290/


All Articles