windows java, System.loadlibrary("someDLLFile") 得到未统计的链接错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1029559/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:41:31  来源:igfitidea点击:

java, System.loadlibrary("someDLLFile") gets unstatisfied link error

javac++windowsdlljava-native-interface

提问by linusthe3rd

I have written some JNI hooks into a C++ library and created some DLL files for my java server project. Lets say the DLL and jar files are in the same folder under "C:/server"

我已经在 C++ 库中编写了一些 JNI 钩子,并为我的 java 服务器项目创建了一些 DLL 文件。假设 DLL 和 jar 文件位于“C:/server”下的同一文件夹中

I am accessing these DLL files using:

我正在使用以下方法访问这些 DLL 文件:

System.loadLibrary("someDLLFile");

in the class that needs the C++ code.

在需要 C++ 代码的类中。

The problem I am running into is when I run this server on my own machine, everything works fine regardless of where I place the "server" folder. But when I give it to a colleague to test, they continually get:

我遇到的问题是,当我在自己的机器上运行此服务器时,无论我将“服务器”文件夹放在哪里,一切都正常。但是当我把它交给同事进行测试时,他们不断得到:

java.lang.UnsatisfiedLinkError no someDLLFile in java.library.path

I want to have the DLL files live in the same folder as the jar files and would prefer not having someone configure their PATH variable.

我想让 DLL 文件与 jar 文件位于同一文件夹中,并且不希望有人配置他们的 PATH 变量。

Why does System.loadLibrary() work on my own machine regardless of the folder's location, but not on another computer?

为什么 System.loadLibrary() 可以在我自己的机器上运行,而不管文件夹的位置如何,而不能在另一台计算机上运行?

回答by Macke

It works because the DLL (or a DLL it depends on, i.e. msvcr90.dll or something) are in the PATH on your machine, but not on the other one.

它之所以有效,是因为 DLL(或它所依赖的 DLL,即 msvcr90.dll 或其他东西)位于您机器上的 PATH 中,但不在另一台机器上。

Either set PATH env-var or the java.library.path property to contain the dir with your file, or store your dll where java finds it by default (Many options here, depending on deployment strategy and platform).

设置 PATH env-var 或 java.library.path 属性以包含文件的目录,或将 dll 存储在 java 默认找到的位置(此处有许多选项,取决于部署策略和平台)。

回答by finnw

One option is to specify the directory in the command line when you start the VM:

一种选择是在启动 VM 时在命令行中指定目录:

java -classpath C:\server -Djava.library.path=C:\server somePackage.Main

Another option is to use System.loadinstead of System.loadLibrary.

另一种选择是使用System.load而不是System.loadLibrary.

URL url = Test.class.getResource("someDLLFile.dll");
String f = new File(url.getFile()).getAbsolutePath();
System.load(f);

The downside is that your program is now dealing with platform-dependent directory names, file extensions etc.

缺点是您的程序现在正在处理依赖于平台的目录名称、文件扩展名等。

回答by OscarRyz

I'm not sure if this is helpful or not, but I have included the following in some projects:

我不确定这是否有帮助,但我在一些项目中包含了以下内容:

http://forums.sun.com/thread.jspa?threadID=707176

http://forums.sun.com/thread.jspa?threadID=707176

To load native libraries.

加载本机库。

And then I just load the bin directory

然后我只加载 bin 目录

    String binPath = new File(".").getAbsolutePath() 
                     + System.getProperty("file.separator") + "bin";

   addDir( binPath );

It works pretty well.

它工作得很好。

But Again, I'm not sure if this is the case or not.

但同样,我不确定是否是这种情况。

回答by Dizzy

Try downloading depends.exe to see if the dll depends on other dlls on the system or not. If it does, then check the other machine, whether such dlls are present or not in Path.

尝试下载depends.exe 以查看该dll 是否依赖于系统上的其他dll。如果是,则检查另一台机器,Path 中是否存在此类 dll。