Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java System.loadLibrary dependencies

Tags:

java

dll

native

A 3rd party jar I use is trying to load a native library using System.loadLibrary. I think what is happening is that one of the libraries being loaded has a dependency on another native library. Pointing -Djava.library.path does not work properly in this case. The instructions from the app's site is put the dlls in the jre/bin directory, but I think this is a really bad idea (especially when trying to deploy to client sites).

So, this question really is 2 parts.

  1. Does it make sense that if a native lib tries to load another native lib that -Djava.library.path does not work?

  2. Is there a good solution for working around this problem? I guess I could explicitly call System.loadLibrary on all of the dlls (I'm not even sure if this would work), but I would need to make sure to call them in the correct order otherwise I'll have the same problem.

EDIT: I think this makes sense that it is happening, and the best solution I've read so far is to use dependency walker to figure it out and then load them in reverse order...but I'm open to better suggestions...

thanks, Jeff

like image 716
Jeff Storey Avatar asked Dec 13 '10 18:12

Jeff Storey


1 Answers

Yes, it makes sense that native libraries do not use the Java property -Djava.library.path to link to other native libraries.

Some possible approaches:

  • The third-party jar is fixed to load its own dependencies, relying on java.library.path.
  • Your code loads the DLLs required by the third-party jar in a reverse topographic sort. However, this makes your code specify the dependencies of the third-party jar. Those dependencies might change.
  • You use the OS-specific DLL search path (e.g., using LD_LIBRARY_PATH on Unix/Linux/Mac, or PATH on Windows). However, this might require a startup script.
like image 173
Andy Thomas Avatar answered Oct 26 '22 15:10

Andy Thomas