Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI dependent libraries

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

like image 325
Jeff Storey Avatar asked Jul 06 '09 13:07

Jeff Storey


3 Answers

  • 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.
like image 64
Vivek Avatar answered Oct 23 '22 01:10

Vivek


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: How to make a JAR file that includes DLL files?

like image 24
Liron Yahdav Avatar answered Oct 23 '22 03:10

Liron Yahdav


This helped me a lot. Also managed loading a JNI dll built using cygwin:

first:

/* conditioned if OS is windows because also need it to work in Linux env. */ 
System.loadLibrary("cygwin1"); 

then:

System.loadLibrary("mylib"); 

On windows, This requires either setting the java.library.path to match both libraries locations.

If runnning from Eclipse, this setting may be replaced by "Native Libraries Location" in java build path (in JRE libraries settings).

However, still finding this a bit tricky.

like image 2
user3448399 Avatar answered Oct 23 '22 03:10

user3448399