如何在 Java 中配置 .dll 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2082026/
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
How to configure .dll file in Java?
提问by Yatendra Goel
I am using Jacobjar file in my java application.
我在我的 Java 应用程序中使用Jacobjar 文件。
This Jacob jar file comes with a .dll file. I have added Jacob jar file to my classpath. But when I execute my application a runtime error occurs as
这个 Jacob jar 文件带有一个 .dll 文件。我已将 Jacob jar 文件添加到我的类路径中。但是当我执行我的应用程序时会发生运行时错误
"couldn't load jacob-1.15-M3-x86.dll file"
How can I load this .dll file?
我怎样才能加载这个 .dll 文件?
Edited:=================================================================================
编辑:================================================ ==================================
I had set the "path" environment varaible to the dir that contains my .dll file and loading that .dll file as follows
我已将“路径”环境变量设置为包含我的 .dll 文件的目录并按如下方式加载该 .dll 文件
static {
System.loadLibrary("jacob-1.15-M3-x86.dll");
}
but the following error occured
但发生了以下错误
java.lang.UnsatisfiedLinkError: no jacob-1.15-M3-x86.dll in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at TemplateClass.TemplateClass.<clinit>(TemplateClass.java:14)
回答by Carl Smotricz
Ah, that's not a compilation error but a runtime error.
啊,这不是编译错误,而是运行时错误。
My guess would be that your DLL needs to be on the PATH
. Not CLASSPATH
, but PATH
, because that's where Windows looks for DLLs. Try either extending your PATH to include the location of your DLL, or do what many other people do: Dump the DLL into \Winnt\System\System32
or whatever the system directory is called on your box. Wherever all the other DLLs are, in other words.
我的猜测是您的 DLL 需要在PATH
. 不是CLASSPATH
,但是PATH
,因为那是 Windows 寻找 DLL 的地方。尝试扩展您的 PATH 以包含您的 DLL 的位置,或者执行许多其他人所做的操作:将 DLL 转储到\Winnt\System\System32
您的机器上调用的任何系统目录中。换句话说,所有其他 DLL 在哪里。
Update
更新
The error message you post, thankfully, is pointing out the exact problem. You can solve it by putting the directory containing your DLL into java.library.path
This Sun forum thread shows a nice example: http://forums.sun.com/thread.jspa?threadID=627890
谢天谢地,您发布的错误消息指出了确切的问题。您可以通过将包含您的 DLL 的目录放入java.library.path
这个 Sun 论坛帖子中来解决这个问题,这是一个很好的例子:http: //forums.sun.com/thread.jspa?threadID=627890
Actually, that's a lot less clean than it should be; this seems to be one of the "shadier" areas in Java. The thread wanders around a lot, I do advise you to read all the way through to see some problems and solutions. I think you'll be able to succeed with a little trial and error.
实际上,这比应有的干净得多;这似乎是 Java 中的“阴影”区域之一。该线程徘徊了很多,我建议您一直通读以查看一些问题和解决方案。我认为您可以通过一些反复试验来取得成功。
回答by Chris Dail
The 'jacob-1.15-M3-x86.dll' needs to be in a place where your the operating system can find it. You have a few options here:
'jacob-1.15-M3-x86.dll' 需要位于您的操作系统可以找到它的地方。您在这里有几个选择:
You can place the .dll file in the directory you started your application from. If you have a batch script to start your application, it would be that directory. If you are starting in some sort of application server, it would typically be the 'bin' directory.
You can place the .dll file somewhere in the %PATH% environment variable. I may be easier to just update your PATH environment variable to include the directory that contains your .dll file.
Another option is to place your .dll into the %SystemRoot%\system32 directory. Usually this is 'C:\Windows\system32'. This option is not usually recommended unless it is a shared library like the MSCVRT runtime.
您可以将 .dll 文件放在启动应用程序的目录中。如果你有一个批处理脚本来启动你的应用程序,它就是那个目录。如果您从某种应用程序服务器开始,它通常是“bin”目录。
您可以将 .dll 文件放在 %PATH% 环境变量中的某个位置。我可能更容易更新您的 PATH 环境变量以包含包含 .dll 文件的目录。
另一种选择是将您的 .dll 放入 %SystemRoot%\system32 目录中。通常这是“C:\Windows\system32”。通常不推荐使用此选项,除非它是像 MSCVRT 运行时这样的共享库。
One other possible issue you might have. If the .dll is compiled as 32-bit, then you must be running in the 32-bit Java runtime. Likewise, if it is a 64-bit .dll it needs to be run in a 64-bit JRE.
您可能遇到的另一个可能的问题。如果 .dll 被编译为 32 位,那么您必须在 32 位 Java 运行时中运行。同样,如果它是 64 位 .dll,则需要在 64 位 JRE 中运行。
回答by duffymo
回答by barjak
Other options :
其他选择:
- set the property java.library.path to the directory containing the dll. Example : java -Djava.library.path="path/to/directory/containing/the/dll" -jar appli.jar
- in the code, load the dll explicitly, with System.load.
- 将属性 java.library.path 设置为包含 dll 的目录。示例:java -Djava.library.path="path/to/directory/ contains/the/dll" -jar appli.jar
- 在代码中,使用 System.load 显式加载 dll。
回答by karla
I had the same problem.
我有同样的问题。
I see that the question is not "answered", so maybe none of the options above worked.
我看到这个问题没有“回答”,所以上面的选项可能都不起作用。
One of my last hypothesis was that the Jacob.dll is missing its dependency.
我的最后一个假设是 Jacob.dll 缺少其依赖项。
What I did was to get dependand check if all the dependence, used by Jacob are loaded. Of course this works for Windows.
我所做的是获取依赖并检查 Jacob 使用的所有依赖是否已加载。当然,这适用于 Windows。
Cheers!
干杯!
回答by Andrew Landsverk
When you use System.loadLibrary()
don't include the .dll
at the end.
使用时System.loadLibrary()
不要.dll
在末尾包含。
Also, if you are not setting java.library.path
to point to the folder containing the DLL then the DLL should be in the directory where you launch your Java application from.
此外,如果您没有设置java.library.path
指向包含 DLL 的文件夹,那么 DLL 应该位于您从中启动 Java 应用程序的目录中。