Loading a Linux .so File at Java Runtime
Asked Answered
T

4

10

I'm trying to load a linux .so file at runtime in Java, but I'm getting an UnsatisfiedLinkError. I'm passing in the -Djava.library.path=/Users/tom/codebase/jni/dist VM argument when running the below java main from my Test.class. The libSample.so file is 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

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 to use try the direct approach (using System.load) and got the below results if these help any Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/tom/codebase/jni/dist/libSample.so: no suitable image found. Did find: /Users/tom/codebase/jni/dist/libCiscoEnergyWiseJni.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00

Tweedsmuir answered 8/11, 2011 at 22:53 Comment(4)
Does your library depend on another library called libCiscoEnergyWiseJni ?Aceous
(if so, you need to append :/the/folder/where/its/installed to the end of java.library.path)Aceous
no sorry that was a typo...fixed nowTweedsmuir
I am facing the exact same problem. Were you able to find a solution?Kolomna
S
16

Libraries on Linux are often named in the pattern libXXX.so, and I believe Java follows that convention. So System.loadLibrary("Sample") may be looking for libSample.so. You can verify this by making a quick test program to call System.mapLibraryName and checking the output.

To resolve the issue, assuming this is in fact the problem you're having, you can either rename your library file or use System.load (not System.loadLibrary), which will load the library specified by the exact filename you pass it, without any transformations. The latter method is not portable across platforms, though.

Strength answered 8/11, 2011 at 23:21 Comment(4)
Thanks for the response David. The file name is actually libSample.so, I've edited my first question...sorry for the confusionTweedsmuir
I did try the direct approach (using System.load) and got the below results if these help any Exception in thread "main" java.lang.UnsatisfiedLinkError: /Users/tom/codebase/jni/dist/libSample.so: no suitable image found. Did find: /Users/tom/codebase/jni/dist/libCiscoEnergyWiseJni.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x00Tweedsmuir
@c12: I suggest editing that information into the question for other answerers.Strength
THANK YOU, really, 2 hours and 10 post later I found the solution. The "libXXX" prefix.Germinate
I
0

Try using

Runtime.getRuntime().load(resource);
Inaccurate answered 28/12, 2011 at 9:5 Comment(0)
A
0

I faced the same issue in Linux and solved by setting the LD_LIBRARY_PATH variable

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

Hope this helps

Animalcule answered 27/10, 2016 at 5:47 Comment(0)
G
-1
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);
        }
    }
}
Guttate answered 5/11, 2014 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.