-->

JAVA/JNI - Load native DLL with circular dependenc

2019-09-09 16:16发布

问题:

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 turns need 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 ?

Thanks for your help.

jpsi

Details:

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 Dependendcy 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.

Please tell me if you need more information.

Again, thanks for any help !!

回答1:

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.