0

Essentially I want to zip up collection of shared objects in a jar file, unzip them to a temporary directory at runtime, and load from there. Say for example I have the libraries:

  • libApp.so
  • libBasicPlugin.so => libApp.so
  • libPlugin1.so => libApp.so, libBasicPlugin.so, libPlugin2.so
  • libPlugin2.so => libApp.so, libBasicPlugin.so

Java unzips them to an arbitrary, unique temporary directory $USER_HOME/.my-app/2011_12_05_001/, and then loads libApp.so, which in turn uses dlopen to load libPlugin1 and libPlugin2 (libBasicPlugin isn't explicitly loaded). The problem is that libPlugin1 doesn't know where to find libBasicPlugin and libPlugin2.

Here are the various solutions I have tried/considered:

  1. Putting the libraries in a known location and setting LD_LIBRARY_PATH in a sh file before invoking Java. This works but precludes packaging the libraries in a jar
  2. Using Java's System.load to "preload" the dependencies. This is the approach I've seen suggested, but only seems to work if you know a priori what plugins are going to be loaded and know their dependencies. Unless you iterated over all the files in a directory, loading them until you stop getting UnsatisfiedLinkExceptions...
  3. Somehow tell the shared object that its dependencies will be in the same directory (via DT_RPATH?). This is what I think I would like in an ideal world, but it seems the best you can do is set the library location relative to the executable, which in this case is /usr/lib/jvm/.../java.
  4. Statically link each library with its dependencies. I'm worried this would result in two copies of libPlugin2 (a dynamic one and a static one) which has caused all sorts of problems for us in the past.

On Windows, calling SetDllDirectory before loading the plugins solves this nicely. Are there any solutions I am overlooking/misunderstanding? Am I going about this entirely the wrong way?

kylewm
  • 634
  • 6
  • 21

1 Answers1

0

You could set the LD_LIBRARY_PATH env var from within the Java process itself.

Johan Boulé
  • 1,936
  • 15
  • 19
  • Ah thank you, I had the same thought, but Linux caches the LD_LIBRARY_PATH when you first run the application -- it cannot be modified at runtime. http://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes – kylewm Dec 05 '11 at 20:24