tags:

views:

468

answers:

3

Hi, I'm running a library via JNI (I didn't write it), and internally it calls another DLL. I get an error saying "Can't find dependent libraries" unless I put the path of the other DLL on the system PATH variable (I'm on Windows XP). I'd like to be able to handle this on the java command line, and I've already tried adding it to -Djava.library.path and to the classpath, neither which worked (I expected -Djava.library.path to work but not classpath, but neither worked). Is there a way to do this?

thanks,

Jeff

+2  A: 
  • If you have a DLL name 'MyNativeDLL.dll' then you should use 'MyNativeDLL' in your LoadLibrary call.
  • Use Dependency Walker to check if there are any files required by MyNativeDLL.dll
  • If there are, include them in the same folder as MyNativeDLL.dll - one you get it working try putting the additional required files in System32 folder.
Vivek
that would work, but I'd like to have the dependent DLLs be in any folder they want and then just reference that folder. is that possible?
Jeff Storey
we had similar situation where we had to load OurJNI.dll and OurNative.dll using the LoadLibrary call. Again, the order in which they are loaded matters. We couldn't find any other way to do this.
Vivek
Right - welcome to DLL hell. In our case, I'm only loading A.dll which internally references B.dll so B should always be loaded after A. I was just trying to avoid modifying the PATH variable.
Jeff Storey
A: 

I was able to get this to work without putting any DLLs on the PATH by using System.load() on all DLLs in reverse dependency order. Just to be clear, I was calling System.load() on all dependent DLLs, not just JNI DLLs. You don't have to call System.load() on DLLs that come with Windows (they're on the PATH).

I was doing this in a web app where a jar included DLLs that were getting unpacked. Your situation seems simpler, so I believe it should work. I generally followed the solution here: http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files

Liron Yahdav
A: 

I've successfully injected a folder into the PATH variable using JNA. This can be used as a workaround if you want to deploy your dependent DLLs alongside your application without tainting the global environment or messing with explicit DLL load order.

However it's not clear to me how the classloader lifecycle affects this. I've only tried this technique under the NetBeans module system, but if you look through the ClassLoader class code for loadLibrary, you'll see that it caches some path variables. It may or may not be necessary to create a new classloader to load the library on.

The downside is you need to use JNA or JNI. Also, it seems like a pretty gross hack. See here for an example of how to set an environment variable using JNA.

gibbss