What becomes the search path of a .dll loaded with System.loadLibrary() in java?

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?

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


    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

  • MSDN "Dynamic-Link Library Search Order"
  • MSDN AddDllDirectory function
  • MSDN SetDllDirectory function
  • SetDllDirectory inheritance issue
  • 链接地址: http://www.djcxy.com/p/44502.html

    上一篇: 由插件引用的DLL的搜索路径

    下一篇: 在java中用System.loadLibrary()加载的.dll的搜索路径是什么?