Java LoadLibrary unresolved dependency but dependent dll is in same directory(Java LoadLibrary 未解决的依赖关系,但依赖的 dll 在同一目录中)
问题描述
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.
If I write:
System.loadLibrary("jzmq");
I get an UnsatisfiedLinkError
of Can't find dependent libraries
. However if instead I say
System.loadLibrary("libzmq");
System.loadLibrary("jzmq");
Then there is no problem and jzmq.dll loads successfully.
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!
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: 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: 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 在同一目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java LoadLibrary 未解决的依赖关系,但依赖的 dll 在同一目录中
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01