2

I'm having some problems loading 2 libs where one depends on another in Linux. Let's say I have 2 libs, libA.so and libB.so, libB.so depends on libA.so (calling functions from it).

I need to load libB.so from Java via JNI and call some native methods from it.

SO what I'm trying to do is:

static {
    System.loadLibrary(A);
    System.loadLibrary(B);
}

(Both libraries reside in java.library.path).

Under Win32, it works fine - B.dll sees that A.dll is already loaded, and doesn't try to load it itself (using PATH lookup).

While on Linux, it doesn't work. Additional logging shows, that System.loadLibrary(A); executes correctly, and libA.so is getting loaded fine, then, when we I try to load B, it looks for library libA.so in the LD_LIBRARY_PATH, and it fails (both libs are in java.library.path, but NOT in LD_LIBRARY_PATH).

Does someone why does it happen? Is it related to the way Linux runtime linking works?

I see a lot of way to work it around, but first want to understand the bottom line of that.

Thanks, Mikhail

Zorkus
  • 484
  • 1
  • 4
  • 13
  • What does the second line fail with? – fge Dec 22 '11 at 22:45
  • Saying that the libA.so can't be found. I have actually already solved this problem. The issue was: _both_ libA and libB should be linked using -Wl,soname flag. – Zorkus Jan 03 '12 at 00:55
  • Nice work Zorkus. As a friendly reminder, can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. – Zecas May 28 '12 at 13:28
  • Only libraries that implement native methods should be loaded with System.loadLibrary(). The dependent libs should be on the LD_LIBRARY_PATH the get resolved correctly. The solution you are using is probably static linking the dependent library which will work fine if your not planning on using a library that is provided by your distro. – Alex Barker Dec 03 '13 at 23:24

0 Answers0