0

I try to load C++ code in my Java project with JNI. I have many several DLL to load, and unfortunately there is a cyclic dependency between two of them: dll A needs dll B which in turn needs dll A! I know it is a bad programming design to have circular dependencies between DLL, but in my project the C++ code is a black box to me.

Is there any way to load DLL with a cyclic dependency?

My code is quite simple:

System.loadLibrary("myDLLA"); // needs dll B to be loaded!
System.loadLibrary("myDLLB"); // needs dll A to be loaded!
System.loadLibrary("myDLLC"); // needs dll B
// then call my native method implemented in dll C

The Java library path is OK and contains the two DLL (it is given as VM argument, I dumped it and checked it at run time too). The cyclic dependency was confirmed by Dependency Walker.

The error is :

java.lang.UnsatisfiedLinkError: E:\...\myDLLA.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1928)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)

My project is developed in Eclipse (Helios) as a dynamic web project deployed on a tomcat 6 server.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
jpsi
  • 1
  • 1

1 Answers1

0

On Windows, the DLL loader will follow the PATH to resolve external references. You can add the directory of myDLLB.dll to PATH globally (through System properties-> advanced), or on command line which launches your Java app (set or xset), or from your Java code.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • Thanks for your answer. I tried it but it did not work (same error). From what I understand, it is not a problem of where to find the dll (the java.library.path is OK anyway, but I tried too with the directory in the PATH env var), but of the order of loading: System.loadLibrary needs to load a dll AFTER its dependencies, but here there is a cyclic dependency... – jpsi Mar 26 '13 at 15:04
  • Well, I don't know how you set the PATH environment variable, and yes, it might be tricky. My simple test shows that setting CurrentDirectory inside the program does not effect the search path for native lib loader, and I did not try to modify PATH inside the program. But setting the PATH environment variable to include the directory of myDLLA.dll, I could loadLibrary("myDLLB") with no problem. – Alex Cohn Mar 27 '13 at 16:30
  • How did you make your simple test? – jpsi Mar 29 '13 at 14:06
  • After you change system properties, you shoud reboot or at least logout /login to have the change effect your program. I used Eclipse, which allows to override environment to run a project. Note that you can also put the DLL in program's current directory (but changing user.dir to point to the DLL after start did not help). – Alex Cohn Mar 29 '13 at 20:24