javac++windowsopencvjava-native-interface

How to solve "java.lang.UnsatisfiedLinkError: Can't find dependent libraries" without System32?


I'm working on a Java project on Eclipse, which uses C++ OpenCV libraries via JNI. Some image processing algorithms are implemented with OpenCV on the native side, and I wish to use them from java using JNI.

I have built a C++ DLL project to link to Java, which resulted in a MyLibrary.dll file. I compiled OpenCV with GCC 6.3 compiler and I compiled the C++ code with the same GCC 6.3 compiler on Eclipse CDT (along with MinGW Linker). I also checked if there are any dependency issues using Dependency Walker. I had no errors thus far.

Afterwards, I tried to load the library from Java code as follows:
System.loadLibrary("MyLibrary")

I have set the path with -Djava.library.path=path\to\MyLibrary, and and made sure that JVM knows the address of the native libraries. I also added the required OpenCV libraries next to MyLibrary.dll.

However I get the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: MyLibrary.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
...

Then the problem goes away, when I move the dependent OpenCV libraries into the System32 folder.

My question is; how can I solve this issue without moving the required DLL files into the System32 folder?


Solution

  • The best practice for this issue is to load dependent libraries by yourself with calling the library load method:

    System.loadLibrary("opencv_1");
    System.loadLibrary("opencv_2");
    ...
    

    After you load dependent libararies now you can safely load your own dll file with the same way:

    System.loadLibrary("MyFile");
    

    This should resolve the can't file dependent libraries error.

    Another workaround (no the best practice) is to copy dependent dll files (in your case opencv dlls) to System32 folder.

    Why this is happening?

    I think, when you set java.library.path argument, you are responsible to load dependent libraries of the library, not the OS itself. I'm not sure to be honest.

    As mentioned in the How to fix an UnsatisfiedLinkError (Can't find dependent libraries) in a JNI project , you can check your path by adding -XshowSettings:properties -version argument to the virtual machine.