I am currently maintaining a Java communication library which wraps the functionality offered by some dll via JNI. At some point, in Java, I need to load the JNI wrapper to forward my requests and finally call the native library. Currently, this is accomplished by calling
System.loadLibrary("MyLibrary");
As stated here, this should always find MyLibrary
if it is placed somewhere within java.library.path
. Currently, my java.library.path
seems to include some Java specific folders as well as all directories specified in the %PATH% environment variable:
C:\Program Files\Java\jdk1.8.0_45\bin;
C:\Windows\Sun\Java\bin;
C:\Windows\system32;
C:\Windows;
C:\Program Files\ImageMagick-6.9.0-Q16;
C:\ProgramData\Oracle\Java\javapath;
C:\Windows\system32;
C:\Windows;
C:\Windows\System32\Wbem;
C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;
C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;
C:\texlive\2014\bin\win32;
C:\MyFolder\Common32;
C:\MyFolder\Common64;
C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\;
C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\Best Practices Analyzer\;.
My problem now is that even though MyLibrary
is placed in C:\MyFolder\Common64;
the above loadLibrary
call yields an UnsatisfiedLinkError
and I cannot seem to understand why. It is however found when I put it into the System32
folder, or if I instead call load
while specifying the path absolutely:
System.load("C:\\MyFolder\\Common64\\MyLibrary.dll");
I tried to mess around with the java.library.path
during runtime using the sys_path
trick suggested in the answers given here and here. The following works flawlessly:
System.setProperty("java.library.path", "C:\\MyFolder\\Common64\\" );
Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
fieldSysPath.setAccessible( true );
fieldSysPath.set( null, null );
System.loadLibrary("MyLibrary");
So, if I replace the entire java.library.path
property with a custom path the dll is successfully loaded. However, this is not the desired behavior, as I want to find the dll dynamically by adding the correct directory to %PATH%. Furthermore, adding my custom path to java.library.path
like so
String curJavaLibraryPath = System.getProperty("java.library.path");
System.setProperty("java.library.path", curJavaLibraryPath + ";C:\\MyFolder\\Common64\\" );
also does not work.
Currently I am trying to make this work on a Win7 64-bit machine. My dll is also compiled as x64, if that's relevant.
The general procedure works flawlessly when I compile my Java library in x86 mode and copy the according JNI dll to C:\MyFolder\Common32\
and add that directory to %PATH%.