user2841047
user2841047

Reputation: 71

Unable to load so file from Java in Eclipse On Ubuntu

I have some code that tries to load a C library as follows :-

public ThreadAffinity() {
    ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}

However I get the following error when trying to build the project; The error I get is as follows :-

UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:166)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:239)
    at com.sun.jna.Library$Handler.<init>(Library.java:140)
    at com.sun.jna.Native.loadLibrary(Native.java:393)
    at com.sun.jna.Native.loadLibrary(Native.java:378)
    at com.threads.ThreadAffinity.<init>(ThreadAffinity.java:11)
    at com.threads.ThreadAffinity.main(ThreadAffinity.java:45)

The current working directory is the root of the project and thats where the so file is located. I also tried modifying the LD_PRELOAD variable to point to my so file; however the error persists.

It works just fine on my OSX where the dylib is located exactly where the so file is currently(project root). What am I doing wrong?

Upvotes: 0

Views: 950

Answers (2)

Simon Black
Simon Black

Reputation: 923

From the exception:

UnsatisfiedLinkError: Unable to load library 'libctest': liblibctest.so: cannot open shared object file: No such file or directory

It implies you used something like:

public ThreadAffinity() {
    ctest = (CTest) Native.loadLibrary("libctest", CTest.class);
}

and not:

public ThreadAffinity() {
    ctest = (CTest) Native.loadLibrary("ctest", CTest.class);
}

hence you see the JNA added prefix of lib and postfix of .so added to libctest (liblibctest.so)

Upvotes: 2

technomage
technomage

Reputation: 10069

LD_PRELOAD is used when you want to prefer one particular version of the same shared library over another, which doesn't apply here.

Define jna.library.path to point to your project root, and JNA should be able to find it.

Also make sure your library has been built as libctest.so and wasn't inadvertently named libctest.dylib.

Upvotes: 1

Related Questions