Search code examples
javadlljarjava-native-interfaceshared-libraries

How to load a library that depends on another library, all from a jar file


I would like to ship my application as a self-contained jar file. The jar file should contain all the class files, as well as two shared libraries. One of these shared libraries is written for the JNI and is essentially an indirection to the other one (which is 100% C).

I have first tried running my jar file without the libraries, but having them accessible through the LD_LIBRARY_PATH environment variable. That worked fine.

I then put the JNI library into the jar file. I have read about loading libraries from jar files by copying them first to some temporary directory, and that worked well for me (note that the 100% C library was, I suppose, loaded as before).

Now I want to put both libraries into the jar, but I don't understand how I can make sure that they will both be loaded. Sure I can copy them both to a temporary directory, but when I load the "indirection" one, it always gives me:

java.lang.UnsatisfiedLinkError: /tmp/.../libindirect.so: /libpure.so: cannot open shared object file: No such file or directory

I've tried to force the JVM to load the "100% C" library first by explicitely calling System.load(...) on its temporary file, but that didn't work better. I suspect the system is looking for it when resolving the links in libindirect.so but doesn't care about what the JVM loaded.

Can anyone help me on that one?

Thanks


Solution

  • One way would be to spawn another Java process from the first, generating the appropriate invocation script.

    1. The jar is invoked by the user
    2. The libraries are extracted to a temp directory
    3. A (bash) script is written to the temp directory
      • this sets/exports the necessary environment variables
      • this launches the second JRE instance
    4. The code makes the script executable
    5. The code invokes the script

    I know, spawning two JRE instances to launch one app would not be my first choice either.