Search code examples
javamacosjna

search paths where one native library depends on another


I'm using JNA and Java but I think this question affects any native-to-nonnative bridge.

I have a Java application which relies on lib1.dylib, and lib1.dylib relies on lib2.dylib.

I want to put everything inside of my .app file on Mac. I can easily put lib1.dylib inside and set java.classpath (or NativeLibrary.addSearchPath()) to tell the JVM where to find lib1.dylib. The trouble is, I don't know how to communicate that lib1.dylib's dependencies are also in the location I provided. The result is that lib1 is loaded fine, but then lib2 can't be found since it's not in the operating system's library path.

Anyone know how I can overcome this problem? I imagine it must come up plenty in big projects with large numbers of shared libraries.


Solution

  • I've come across this problem before, and have just run into it again today. You may be able to get around it by adding the VM argument "-Djava.library.path=/path/to/other/libs", but I seem to remember Java only uses that to search for the intial library and then uses the system PATH to look for any dependencies.

    A few solutions I've tried before:

    1) Use System.load(absolutePath) on the dependent library before loading your library. Doesn't make your program ultra-portable though, unless you always know where that library is going to be.

    2) In a case where lib1 depends on lib2, I actually used SetCurrentDirectory (Windows, not sure of the Mac equivalent) in the native code before it linked to any of the dependent libs, and that seemed to work. Again, requires knowing where the other libs are.

    3) On Windows, could dump the dependent libraries in c:\windows\system32, and it finds them.

    A few helpful posts on a similar topic (Windows-specific, but I think the problem is the same):

    http://www.realityinteractive.com/rgrzywinski/archives/000219.html http://www.velocityreviews.com/forums/t387618-jni-library-path.html