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 在同一目录中


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01