java.lang.UnsatisfiedLinkError 在 java.library.path 中没有 *****.dll [英] java.lang.UnsatisfiedLinkError no *****.dll in java.library.path
问题描述
如何在我的 Web 应用程序中加载自定义 dll 文件?我尝试了以下方法:
How can I load a custom dll file in my web application? I've tried the following:
- 复制了
system32
文件夹中所有必需的 dll 并尝试在Servlet
构造函数System.loadLibrary
中加载其中之一 - 将所需的 dll 复制到
tomcat_home/shared/lib
和tomcat_home/common/lib
- Copied all required dlls in
system32
folder and tried to load one of them inServlet
constructorSystem.loadLibrary
- Copied required dlls into
tomcat_home/shared/lib
andtomcat_home/common/lib
所有这些dll都在网络应用程序的WEB-INF/lib
All these dlls are in WEB-INF/lib
of the web-application
推荐答案
为了使 System.loadLibrary()
工作,库(在 Windows 上,DLL)必须位于某个目录中在您的 PATH
或 在 java.library.path
系统属性中列出的路径上(因此您可以像 java 一样启动 Java -Djava.library.path=/path/to/dir
).
In order for System.loadLibrary()
to work, the library (on Windows, a DLL) must be in a directory somewhere on your PATH
or on a path listed in the java.library.path
system property (so you can launch Java like java -Djava.library.path=/path/to/dir
).
此外,对于loadLibrary()
,您指定库的基本名称,末尾没有.dll
.因此,对于 /path/to/something.dll
,您只需使用 System.loadLibrary("something")
.
Additionally, for loadLibrary()
, you specify the base name of the library, without the .dll
at the end. So, for /path/to/something.dll
, you would just use System.loadLibrary("something")
.
您还需要查看您得到的确切 UnsatisfiedLinkError
.如果它说:
You also need to look at the exact UnsatisfiedLinkError
that you are getting. If it says something like:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no foo in java.library.path
然后它在您的 PATH
或 java.library.path
中找不到 foo 库 (foo.dll).如果它说:
then it can't find the foo library (foo.dll) in your PATH
or java.library.path
. If it says something like:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.example.program.ClassName.foo()V
那么库本身就有问题,因为 Java 无法将应用程序中的本机 Java 函数映射到其实际的本机对应物.
then something is wrong with the library itself in the sense that Java is not able to map a native Java function in your application to its actual native counterpart.
首先,我会在您的 System.loadLibrary()
调用周围放置一些日志,以查看它是否正确执行.如果它抛出异常或者不在实际执行的代码路径中,那么你总是会得到上面解释的后一种UnsatisfiedLinkError
.
To start with, I would put some logging around your System.loadLibrary()
call to see if that executes properly. If it throws an exception or is not in a code path that is actually executed, then you will always get the latter type of UnsatisfiedLinkError
explained above.
作为旁注,大多数人将他们的 loadLibrary()
调用放入具有本机方法的类中的静态初始化块中,以确保它始终只执行一次:
As a sidenote, most people put their loadLibrary()
calls into a static initializer block in the class with the native methods, to ensure that it is always executed exactly once:
class Foo {
static {
System.loadLibrary('foo');
}
public Foo() {
}
}
这篇关于java.lang.UnsatisfiedLinkError 在 java.library.path 中没有 *****.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!