【问题标题】:Can't load personal dll with jna from netbeans无法从 netbeans 加载带有 jna 的个人 dll
【发布时间】:2014-01-31 17:15:52
【问题描述】:

我正在使用 netbeans 7.4 / jdk 1.7u51

我从官方网站下载了 JNA 的 jar,版本为 4.0.0。

我有一个内部开发的 DLL,它的接口是纯 C 语言,可以很好地加载 ctypes 中的 python。此 dll 在 Visual 2010 的发行版中编译,其运行时在路径中。

D:\fl006\Downloads>dir D:\deploy\SpotLight\spotlight-1488\PasanBusLibrary.dll
 Directory of D:\deploy\SpotLight\spotlight-1488

29.01.2014  11:13         1'690'112 PasanBusLibrary.dll

我尝试用 jna 在 java 中加载它:

public interface CLibrary extends Library {
     (...snip...)
    void pasanIpcInitializeLibrary(String xClient, String xBusName, int xTimeout);
    void pasanIpcTerminateLibrary();
}

public static void main(String[] args) {
    NativeLibrary.addSearchPath("PasanBusLibrary","D:\\deploy\\SpotLight\\spotlight-1488");
    CLibrary Bus = (CLibrary) Native.loadLibrary("PasanBusLibrary",CLibrary.class);
(... snip ...)    

这基本上是从自定义位置加载的书外标准 dll 加载。

激活 jna 调试时,我看到以下内容:

run:
Looking in classpath from sun.misc.Launcher$AppClassLoader@714a8f44 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/D:/code/perso/TestWrapperBus/jna-4.0.0.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Looking for library 'PasanBusLibrary'
Adding paths from jna.library.path: null
Trying D:\deploy\SpotLight\spotlight-1488\PasanBusLibrary.dll
Adding system paths: []
Trying D:\deploy\SpotLight\spotlight-1488\PasanBusLibrary.dll
Looking for lib- prefix
Trying libPasanBusLibrary.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader@714a8f44 for PasanBusLibrary
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'PasanBusLibrary': Native library (win32-x86-64/PasanBusLibrary.dll) not found in resource path ([file:/D:/code/perso/TestWrapperBus/jna-4.0.0.jar, file:/D:/code/perso/TestWrapperBus/jna-platform-4.0.0.jar, file:/D:/code/perso/TestWrapperBus/build/classes/])
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
    at com.sun.jna.Library$Handler.<init>(Library.java:147)
    at com.sun.jna.Native.loadLibrary(Native.java:412)
    at com.sun.jna.Native.loadLibrary(Native.java:391)
    at testwrapperbus.TestWrapperBus.main(TestWrapperBus.java:39)

它看起来是通过我给出的位置并以某种方式丢弃它。我尝试了不同的文件夹,我得到了相同的行为,没有明显的文件系统正确问题(所有用户的 dll 都是 RW)

关于我缺少什么的任何线索,我现在有点卡住了......

编辑

  • 如果我加载“msvcrt”,这对 cll printf 很有效
  • 我的 dll 有一些依赖项,它们都托管在 c:\windows\system32 中(标准运行时,动态链接)

【问题讨论】:

  • 您是否让 JNA 为其他 dll(例如您的操作系统的 dll)工作? JNA jar 文件是否在您的类路径中?
  • 是的。我设法使用 c runtime 从文档中运行 printf 示例。我认为这验证了 jna 设置

标签: java dll jna


【解决方案1】:

我的 dll 是 win32 编译,而我使用的是 win64 JDK / JRE。当然,在处理纯java时,我们并不关心,但加载native library需要匹配。

我尝试在 32 位 JRE7 上从命令行运行,它工作正常,所以我很确定在我的 netbeans 中安装 JDK for win32 或重新编译我的 64 位 dll 将解决这个问题。

感谢这个答案:Trying to use DLL from Java (JNA). Unable to load library exception 让我走上了正道

【讨论】:

  • 通过使用 Cygwin 命令:file &lt;filename&gt; 我们可以方便地判断 DLL 是 32 位版本还是 64 位版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多