在 JNA 中加载 DLL 时找不到指定的模块
Specified module could not being found loading DLL in JNA
我有这个 class 初始化程序:
System.setProperty("java.library.path", "C:\Users\lucas\Desktop\libraries");
System.loadLibrary("libTTARCHHelper");
TTARCH_LIBRARY=(TtarchLibrary)Native.loadLibrary(TtarchLibrary.class);
并且DLL位于C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
如果我在 Eclipse 中 运行 我得到 'The specified module could not be found' 如果我 运行 它作为一个 JAR 我得到 'no libTTARCHHelper in java.library.path'.
我该如何解决这些问题?我什至尝试将 DLL 放在 PATH 环境变量的文件夹中。
使用文件直接加载进行完整调试:
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Found library resource at jar:file:/C:/Users/lucas/Desktop/My%20Stuff/Eclipse%20Workspaces/Build%20Paths/jna-5.6.0.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Extracting library to C:\Users\lucas\AppData\Local\Temp\jna-103324076\jna5931694657592960137.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Looking for library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll'
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding paths from jna.library.path: null
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding system paths: []
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll':
The specified module could not be found.
The specified module could not be found.
跟踪的其余部分:
Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:301)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:461)
at com.sun.jna.Library$Handler.<init>(Library.java:192)
at com.sun.jna.Native.loadLibrary(Native.java:646)
at com.sun.jna.Native.loadLibrary(Native.java:630)
at com.test.TTARCHHelper.<clinit>(TTARCHHelper.java:17)
at com.test.main.main(main.java:6)
Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
... 6 more
Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
... 6 more
Suppressed: java.io.IOException: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
at com.sun.jna.Native.extractFromResourcePath(Native.java:1095)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:275)
... 6 more
所以看起来它没有找到它,它抛出一个
java.lang.UnsatisfiedLinkError: The specified module could not be found.
我发现了问题,DLL 是针对错误位的。 Eclipse 运行s on 64 和 JAR 使用 32 位 JVM。我也下载了 32 位 dll,现在当我 运行 JAR 时它可以工作。然而,Eclipse 中的 64 位 dll 不断抛出上述模块异常
JNA 将在 jna.library.path
环境变量中的位置查找库。
一个选项是在 Eclipse 的 运行 配置中将其指定为命令行选项。在运行菜单中,select运行配置。转到 (x)=Arguments 选项卡。在 VM 参数字段中添加:
-Djna.library.path=C:\Users\lucas\Desktop\libraries
您还可以在尝试加载 DLL 之前以编程方式在代码中设置它:
System.setProperty("jna.library.path", "C:\Users\lucas\Desktop\libraries");
正如您稍后在调试日志中指出的那样,它可能找到了 DLL 但无法打开它。可能的原因可能是:
- DLL runtime-dependent 在您系统上的另一个 DLL 上,该 DLL 不在适当的路径中。一个常见的是 Visual C++ Distributable。
您可能需要查阅 DLL 文档以获取更多信息。您有时可以使用 SysInternals 库中的 Process Monitor 来尝试查看打开的内容。
- 具有上述依赖 DLL 的 duplicate/incompatible 版本
- 运行正在运行 64 位 JVM 并尝试打开与 64 位不兼容的 DLL,或者 vice-versa
- 您的 Java 程序可能没有权限读取您的库所在的目录
为了帮助诊断问题,请查看完整的堆栈跟踪。虽然最近的错误消息可能不相关(在“备份”搜索中找不到文件),但如果您查看跟踪,您可能会看到早期尝试打开 DLL 时出现的多个被抑制的异常,这可能会为您提供更多有用的调试信息。
我有这个 class 初始化程序:
System.setProperty("java.library.path", "C:\Users\lucas\Desktop\libraries");
System.loadLibrary("libTTARCHHelper");
TTARCH_LIBRARY=(TtarchLibrary)Native.loadLibrary(TtarchLibrary.class);
并且DLL位于C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
如果我在 Eclipse 中 运行 我得到 'The specified module could not be found' 如果我 运行 它作为一个 JAR 我得到 'no libTTARCHHelper in java.library.path'.
我该如何解决这些问题?我什至尝试将 DLL 放在 PATH 环境变量的文件夹中。
使用文件直接加载进行完整调试:
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Found library resource at jar:file:/C:/Users/lucas/Desktop/My%20Stuff/Eclipse%20Workspaces/Build%20Paths/jna-5.6.0.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Extracting library to C:\Users\lucas\AppData\Local\Temp\jna-103324076\jna5931694657592960137.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Looking for library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll'
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding paths from jna.library.path: null
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Adding system paths: []
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Trying C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: The specified module could not be found.
Aug 26, 2020 12:28:51 AM com.sun.jna.Native extractFromResourcePath
INFO: Looking in classpath from jdk.internal.loader.ClassLoaders$AppClassLoader@c387f44 for C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll
Aug 26, 2020 12:28:51 AM com.sun.jna.NativeLibrary loadLibrary
INFO: Loading failed with message: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll':
The specified module could not be found.
The specified module could not be found.
跟踪的其余部分:
Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:301)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:461)
at com.sun.jna.Library$Handler.<init>(Library.java:192)
at com.sun.jna.Native.loadLibrary(Native.java:646)
at com.sun.jna.Native.loadLibrary(Native.java:630)
at com.test.TTARCHHelper.<clinit>(TTARCHHelper.java:17)
at com.test.main.main(main.java:6)
Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
... 6 more
Suppressed: java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
... 6 more
Suppressed: java.io.IOException: Native library (win32-x86-64/C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll) not found in resource path (C:\Users\lucas\eclipse-workspace\TEST\bin;C:\Users\lucas\Desktop\My Stuff\Eclipse Workspaces\Build Paths\jna-5.6.0.jar)
at com.sun.jna.Native.extractFromResourcePath(Native.java:1095)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:275)
... 6 more
所以看起来它没有找到它,它抛出一个
java.lang.UnsatisfiedLinkError: The specified module could not be found.
我发现了问题,DLL 是针对错误位的。 Eclipse 运行s on 64 和 JAR 使用 32 位 JVM。我也下载了 32 位 dll,现在当我 运行 JAR 时它可以工作。然而,Eclipse 中的 64 位 dll 不断抛出上述模块异常
JNA 将在 jna.library.path
环境变量中的位置查找库。
一个选项是在 Eclipse 的 运行 配置中将其指定为命令行选项。在运行菜单中,select运行配置。转到 (x)=Arguments 选项卡。在 VM 参数字段中添加:
-Djna.library.path=C:\Users\lucas\Desktop\libraries
您还可以在尝试加载 DLL 之前以编程方式在代码中设置它:
System.setProperty("jna.library.path", "C:\Users\lucas\Desktop\libraries");
正如您稍后在调试日志中指出的那样,它可能找到了 DLL 但无法打开它。可能的原因可能是:
- DLL runtime-dependent 在您系统上的另一个 DLL 上,该 DLL 不在适当的路径中。一个常见的是 Visual C++ Distributable。 您可能需要查阅 DLL 文档以获取更多信息。您有时可以使用 SysInternals 库中的 Process Monitor 来尝试查看打开的内容。
- 具有上述依赖 DLL 的 duplicate/incompatible 版本
- 运行正在运行 64 位 JVM 并尝试打开与 64 位不兼容的 DLL,或者 vice-versa
- 您的 Java 程序可能没有权限读取您的库所在的目录
为了帮助诊断问题,请查看完整的堆栈跟踪。虽然最近的错误消息可能不相关(在“备份”搜索中找不到文件),但如果您查看跟踪,您可能会看到早期尝试打开 DLL 时出现的多个被抑制的异常,这可能会为您提供更多有用的调试信息。