如何解决“java.lang.UnsatisfiedLinkError:找不到依赖库”没有System32? [英] How to solve "java.lang.UnsatisfiedLinkError: Can't find dependent libraries" without System32?
问题描述
我正在开发Eclipse上的Java项目,它通过JNI使用C ++ OpenCV库。一些图像处理算法是在本机端使用OpenCV实现的,我希望使用JNI从java中使用它们。
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.
我已经构建了一个链接到Java的C ++ DLL项目,这导致 MyLibrary.dll
文件。我使用GCC 6.3编译器编译了OpenCV,并使用Eclipse CDT上的相同GCC 6.3编译器(以及MinGW Linker)编译了C ++代码。我还使用 Dependency Walker 检查是否存在任何依赖性问题。到目前为止我没有任何错误。
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.
之后,我尝试从Java代码加载库,如下所示:
系统.loadLibrary(MyLibrary)
Afterwards, I tried to load the library from Java code as follows:
System.loadLibrary("MyLibrary")
我用 -Djava.library.path = path \\设置路径\\ to \ MyLibrary
,并确保JVM知道本机库的地址。我还在 MyLibrary.dll
旁边添加了所需的OpenCV库。
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
.
但是我收到以下错误:
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)
...
当我将依赖的OpenCV库移动到 System32
文件夹中时问题就消失了。
Then the problem goes away, when I move the dependent OpenCV libraries into the System32
folder.
我的问题是;如何解决此问题,而无需将所需的DLL文件移动到 System32
文件夹中?
My question is; how can I solve this issue without moving the required DLL files into the System32
folder?
推荐答案
此问题的最佳做法是通过调用库加载方法自己加载依赖库:
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");
...
现在加载相关库后,您可以安全地加载自己的dll文件以同样的方式:
After you load dependent libararies now you can safely load your own dll file with the same way:
System.loadLibrary("MyFile");
这应解决无法文件依赖的库
错误。
另一种解决方法(不是最佳做法)是将依赖的dll文件(在您的情况下为opencv dll)复制到System32文件夹。
Another workaround (no the best practice) is to copy dependent dll files (in your case opencv dlls) to System32 folder.
为什么会发生这种情况?
Why this is happening?
我认为,当你设置 java.library.path $ c时$ c>参数,您负责加载库的依赖库,而不是操作系统本身。我不确定是否诚实。
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.
如如何修复JNI项目中的UnsatisfiedLinkError(无法找到依赖库)
,您可以通过添加 -XshowSettings:properties -version
来检查路径虚拟机的参数。
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.
这篇关于如何解决“java.lang.UnsatisfiedLinkError:找不到依赖库”没有System32?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!