3

I have a system where I have 1 .exe file, 4 .dll files and one jar file where the dependencies are as shown below:

A.exe -> calls -> B.dll -> calls through JNI -> C.jar -> loads with System.loadLibrary("") -> D.dll -> calls E.dll

Now, B through E is one module and all those files are in the same directory. A.exe is an application placed in a different directory which can use several of these modules.

My main problem is that when D.dll tries to load E.dll I get a 'Can't find dependent libraries' java.lang.UnsatisfiedLinkError. If I am standing in the module directory and run C.jar manually there is no such error and D.dll manages to load E.dll just fine.

So, my main question here is: AddDllDirectory(%moduleDir%) is run in A.exe, but how far does it actually get 'inherited'? Does C.jar somehow remove the directory added by this function? Is there a way to expand the dll search path from java, before the System.loadLibrary("") call, such that the loaded .dll inherits this search path?

  1. The java.library.path is set to the module directory when B.dll starts the JVM through JNI
  2. I want to avoid having A.exe altering the PATH environment variable
  3. No changes should be necessary in D.dll or E.dll

Thanks in advance for any answers

2
  • Why do you get a Java excpetion java.lang.UnsatisfiedLinkError when your system library D.dll tries to load the system library E.dll. What's the wording of the exception message? Is it D.dll: %1 is not a valid Win32 application or no D in java.library.path? Commented Sep 9, 2015 at 10:34
  • This is the error message: java.lang.UnsatisfiedLinkError: <moduleDir>\D.dll: Can't find dependent libraries What happens here is that in java when System.loadLibrary("D.dll") is executed, it fails because it cannot find E.dll which D.dll depends on Commented Sep 9, 2015 at 10:49

1 Answer 1

4

Here a short explanation for the error which occur in your case.

Following is assumed

  • Java calls System.loadLibrary("D"); the class is in directory APP_DIR
  • D.dll depends on E.dll, both in directory DLL_DIR

case 1 java.library.path not specified

no D in java.library.path 

case 2 -Djava.library.path=%DLL_DIR%

D.dll: Can't find dependent libraries 

Because Java checks for the presence of D.dll in %DLL_DIR% load the library (using Windows LoadLibrary functionality). Windows tries to find the dependent E.dll which is not found in the PATH and not in the current directory.

case 3 -Djava.library.path=%DLL_DIR% and set PATH=%DLL_DIR%;%PATH%

System.loadLibrary("D"); will be successful 

some additional links about this topic

Sign up to request clarification or add additional context in comments.

2 Comments

After reading the following from your last link: SetDllDirectory("") also affects how child processes loads their DLLs. Good news: It does not affect dynamic linking... Bad news: It affects implicit linking... does this mean that if D.dll loads E.dll using dynamic linking the AddDllDirectory() function called in A.exe is ignored, whereas if D.dll loads E.dll statically it is not?
@MartinRindarøy I can't prove it myself (have never called Java code from a library). But for me the break is at the point where you call the JVM as the library resolving works different here. So the child D.dll will not inherit the settings from A.exe. Maybe you could in the Java code change the current directory to the location of C.jar, then the LoadLibrary from Windows would find the C.dll as it's in the current directory. Best would be do create a small test case for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.