Java LoadLibrary无法解析的依赖项,但依赖的dll在同一目录中 [英] Java LoadLibrary unresolved dependency but dependent dll is in same directory

查看:186
本文介绍了Java LoadLibrary无法解析的依赖项,但依赖的dll在同一目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java加载本机dll时遇到问题,因为它发生在64位Windows 7中。FWIW程序包是ZeroMQ,但重要的是该jar需要加载本机dll,称为jzmq.dll。这又取决于libzmq.dll(和某些标准库)。我已经将两个dll复制到了target / lib并将 java.language.path 设置为target / lib。

I'm having problems with Java loading a native dll, as it happens 64-bit Windows 7. FWIW the package is ZeroMQ but what matters is that the jar requires a native dll to load, called jzmq.dll. This in turn depends on libzmq.dll (and some standard libraries). I've copied both dlls into target/lib and set java.language.path to target/lib.

如果我写:

System.loadLibrary("jzmq");

我得到 UnsatisfiedLinkError 找不到依赖库。但是,如果我改为说

I get an UnsatisfiedLinkError of Can't find dependent libraries. However if instead I say

System.loadLibrary("libzmq");
System.loadLibrary("jzmq");

然后就没有问题,jzmq.dll成功加载。

Then there is no problem and jzmq.dll loads successfully.

在随附的jar中,它只有 System.loadLibrary( jzmq),我希望不必在代码中摆弄依赖项。我尚不清楚为什么依赖性不能自动加载,或者我需要做些什么才能使其正常工作。

Inside the accompanying jar it just has System.loadLibrary("jzmq") and I'd prefer not to have to fiddle with the dependency in my code. It's not clear to me why the dependency doesn't load automatically or what I'd need to do to get it to work properly.

在此先感谢您的帮助!

Thanks in advance for any help!

推荐答案

库的依赖关系是由操作系统而不是Java运行时来解决的。当您在目录中设置 java.library.path 时,Java运行时知道在哪里查找库,但是操作系统仍然找不到依赖项。 Windows要查找您的库,必须在 PATH 环境变量中设置目录。

Dependencies of libraries are resolved by the operating system not by the Java runtime. When you set java.library.path to your directory the Java runtime knows where to look for libraries but the operating system will still not find the dependencies. For Windows to find your library you have to set your directory in the PATH environment variable.

PS:
第一次加载依赖库时起作用的原因是,它将在以后位于进程的地址空间中,Windows会在该地址空间中找到它,而无需在文件系统中找到它。

PS: The reason it works when you load the dependent library first is, that it will be in the process' address space afterward and Windows will find it there and won't need to find it in the file system

PPS:文章说,如果将依赖库放在同一目录中,则会在Windows上自动找到它们,这只能说明事实的一半。起作用的原因是:a)他们在谈论可执行文件的依赖性,而不是其他库; b)当启动没有显式工作目录的可执行文件时,工作目录将是其中包含可执行文件的目录,并且Windows自动添加搜索路径的工作目录(因此将找到同一目录中的库)。

PPS: articles saying that dependent libraries will automatically be found on Windows if you put them in the same directory are only telling half of the truth. The reason this works is that a) they're talking about dependencies of excutables not other libraries and b) when you start an executable without an explicit working directory, the working directory will be the directory that has the executable in it and Windows automatically adds the working directory to the search path (therefore libraries that are in the same directory will be found).

这篇关于Java LoadLibrary无法解析的依赖项,但依赖的dll在同一目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆