Java UnsatisfiedLinkError - 无法加载库 - 在资源路径中找不到本机库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19768453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnsatisfiedLinkError - Unable to load library - Native library not found in resource path
提问by Suzan Cioc
I have the following error at runtime, while trying to run Tess4J:
在尝试运行 Tess4J 时,我在运行时遇到以下错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302': Native library (win32-x86-64/libtesseract302.dll) not found in resource path ([myproject/target/classes/, ...some jars...])
My questions are:
我的问题是:
1) What exactly it tries to find and where?
1)它究竟试图寻找什么以及在哪里?
2) Why is it apparently searches for myproject/target/classes/
directory? I set it nowhere.
2)为什么它显然是在搜索myproject/target/classes/
目录?我没有设置它。
3) Why is it ignores "native directory path" I set for tess4j.jar
in user library descripto in Eclipse? My DLLs are there. If it didn't ignore the path, it would find DLLs.
3)为什么它会忽略我tess4j.jar
在 Eclipse 的用户库描述中设置的“本机目录路径” ?我的 DLL 在那里。如果它没有忽略路径,它会找到 DLL。
4) Why is it apparently prepending DLL name with win32-x86-64/
? I set this nowhere. Is this standard prefix of some API?
4) 为什么它显然在 DLL 名称前面加上win32-x86-64/
? 我没有设置这个。这是某些API的标准前缀吗?
5) What is "resource path"? How to set it?
5)什么是“资源路径”?如何设置?
回答by technomage
Like the error says, it's looking for win32-x86-64/libtesseract302.dll
in java.class.path
. Part of your classpath apparently includes myproject/target/classes
.
像错误说,它是寻找win32-x86-64/libtesseract302.dll
在java.class.path
。您的类路径的一部分显然包括myproject/target/classes
.
The prefix represents the platform and architecture of the shared library to be loaded, which allows shared libraries for different targets to be included in the same archive. If JNA cannot find the requested library name in the system load path, then it attempts to find it within your resource path (extracting it, if necessary). So if you put the DLL in a jar file, you'll need to give it the win32-x86-64
prefix in order for it to load.
前缀代表要加载的共享库的平台和架构,它允许不同目标的共享库包含在同一个存档中。如果 JNA 在系统加载路径中找不到所请求的库名称,那么它会尝试在您的资源路径中找到它(如有必要,提取它)。因此,如果将 DLL 放在 jar 文件中,则需要为其提供win32-x86-64
前缀以便加载。
The "resource path" is nominally your class path; basically anywhere reachable by ClassLoader.getResource()
.
“资源路径”名义上是您的类路径;基本上可以到达的任何地方ClassLoader.getResource()
。
回答by nguyenq
The error stems from your trying to load 32-bit DLLs in 64-bit JVM. The possible solution is switch to 32-bit JVM; alternatively, use 64-bit Tesseract and Leptonica DLLs.
该错误源于您尝试在 64 位 JVM 中加载 32 位 DLL。可能的解决方案是切换到 32 位 JVM;或者,使用64 位 Tesseract 和 Leptonica DLL。
回答by taymedee
Why don't you use JNA API http://www.java2s.com/Code/Jar/j/Downloadjna351jar.htm
to load native library? Once you putted into your project classpath, you add this code
为什么不使用 JNA APIhttp://www.java2s.com/Code/Jar/j/Downloadjna351jar.htm
来加载原生库?放入项目类路径后,添加此代码
NativeLibrary.addSearchPath("libtesseract302", "your native lib path");
make sure you have this libtesseract302.dll file, normally it is located at windows32
folder.
NativeLibrary.addSearchPath("libtesseract302", "your native lib path");
确保你有这个 libtesseract302.dll 文件,它通常位于windows32
文件夹中。
For example, if your libtesseract302.dll file in somewhere c:/abcv/aaa/libtesseract302.dll
then you just set the path like this NativeLibrary.addSearchPath("libtesseract302", "c:/abcv/aaa");
例如,如果您的 libtesseract302.dll 文件在某处,c:/abcv/aaa/libtesseract302.dll
那么您只需像这样设置路径NativeLibrary.addSearchPath("libtesseract302", "c:/abcv/aaa");
I don't know how windows path look like either c:/abcv/aaa
or c:\\abcv\\aaa\\
我不知道 windows 路径是什么样的,c:/abcv/aaa
或者c:\\abcv\\aaa\\
if you want easier way, just put all your necessary dll file into your windows32 folder, JVM will take care of it.
如果您想要更简单的方法,只需将所有必需的 dll 文件放入 windows32 文件夹,JVM 会处理它。
Another issue might be you were not installing the application correctly or the application version is unmatch with your jar version. try to install the latest application and download the latest jar to try again. Hope it helps :)
另一个问题可能是您没有正确安装应用程序或应用程序版本与您的 jar 版本不匹配。尝试安装最新的应用程序并下载最新的jar 再试一次。希望能帮助到你 :)
回答by user2759511
I had the same problem and found that this "resource path" is not set by "native directory path" .
You can however add new folders to it by using "Add External Class Folder" in the Library tab, even if this folder does not contain any class file but native library files(like DLL on Windows)
我遇到了同样的问题,发现这个“资源路径”不是由“本机目录路径”设置的。
但是,您可以通过使用“库”选项卡中的“添加外部类文件夹”向其中添加新文件夹,即使此文件夹不包含任何类文件但包含本机库文件(如 Windows 上的 DLL)
回答by ohgodnotanotherone
A few days ago I ran into the same error message when trying to load a C++ DLL with JNA. It turned out that the cause was a missing DLL that my DLL depended on.
几天前,我在尝试使用 JNA 加载 C++ DLL 时遇到了相同的错误消息。事实证明,原因是我的 DLL 所依赖的 DLL 丢失。
In my case it was the MS Visual Studio 2012 redistributable, which I then downloaded and installed on the machine and the problem was gone. Try using Dependency Walkerto find any missing libraries and install them.
就我而言,它是 MS Visual Studio 2012 可再发行组件,然后我将其下载并安装在机器上,问题就消失了。尝试使用Dependency Walker查找任何缺失的库并安装它们。
回答by Painy James
Had the same issue, sorted with the following lines
有同样的问题,按以下几行排序
System.load("/usr/local/lib/liblept.so.5")
System.loadLibrary("tesseract")
System.load("/usr/local/lib/liblept.so.5")
System.loadLibrary("tesseract")
For your case, it might be different libraries but in the end is pretty much the same: just load the libraries that you need manually.
对于您的情况,它可能是不同的库,但最终几乎相同:只需手动加载您需要的库。
回答by deepak kumar
I was facing similar problem I tried all the answer above but it didn't solved my problem, finally I checked with the source from where I got the dll and found that I didn't had windows redistributable 2012 installed, the dll was build using it, once I installed it the exception was gone.
我遇到了类似的问题我尝试了上面的所有答案,但它没有解决我的问题,最后我检查了我从哪里获得 dll 的源代码,发现我没有安装 windows Redistributable 2012,dll 是使用构建的它,一旦我安装了它,异常就消失了。
回答by Thanis Albert
I had same problem, Unable to load library 'libtesseract302'dll
from C:\Users\xxxxx\AppData\Local\Temp\tess4j\win32-x86-64.
我有同样的问题,无法加载库'libtesseract302'dll
从C:\Users\xxxxx\AppData\Local\Temp\tess4j\win32-x86-64.
After some analysis I found out the problem using dependency walker. The problem was some of the windows.dllare missing in system32. I installed Microsoft Visual C++ 2005,2017-2019 to update dll files in Windows. This solved my problem.
经过一些分析,我使用依赖步行器发现了问题。问题是system32中缺少一些windows.dll。我安装了 Microsoft Visual C++ 2005,2017-2019 来更新 Windows 中的 dll 文件。这解决了我的问题。