Search code examples
javashared-librariesjna

Java: load shared libraries with dependencies


I am wrapping a shared library (written in C) with Java using JNA. The shared library is written internally, but that library uses functions from another external library, which again depends another external library. So the situation is something like this:

ext1 <- ext2 <- internal

I.e. the internal uses external library ext2 which again uses external library ext1. What I have tried is:

System.loadLibrary("ext1");
System.loadLibrary("ext2");
NativeLIbrary.loadLibrary("internal",xxx.class);  

This approach fails with "UnresolvedException" when loading the library "ext2"; the linker complains about symbols which are indeed present in the library "ext1". So it semmes that the System.loadLibrary() function does not make the symbols from "ext1" globally available? When using the stdlib function dlopen() as:

handle = dlopen( lib_name , RTLD_GLOBAL );

All the symbols found in @lib_name will be available for symbol resolution in subsequent loads; I guess what I would like was something similar for the java variety System.loadLibrary()?

Regards - Joakim Hove


Solution

  • OK;

    I have found an acceptable solution in the end, but not without significant amount of hoops. What I do is

    1. Use the normal JNA mechanism to map the dlopen() function from the dynamic linking library (libdl.so).
    2. Use the dlopen() function mapped in with JNA to load external libraries "ext1" and "ext2" with the option RTLD_GLOBAL set.

    It actually seems to work :-)