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

查看:56
本文介绍了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.

在此先感谢您的帮助!

推荐答案

库的依赖性由操作系统解决,而不是由 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.

附注:首先加载依赖库时它起作用的原因是,它之后会在进程的地址空间中,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天全站免登陆