Specified module could not being found loading DLL in JNA

huangapple go评论72阅读模式
英文:

Specified module could not being found loading DLL in JNA

问题

我有这个类的初始化器:

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中运行这个程序,我会得到'找不到指定的模块'的错误,如果我将其打包成JAR文件运行,我会得到'java.library.path中没有libTTARCHHelper'的错误。

我应该如何解决这些问题?我甚至尝试将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.

The rest of the trace:

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使用64位而JAR使用32位的JVM我也下载了32位的DLL文件现在在运行JAR文件时可以工作但是在Eclipse中使用64位的DLL文件仍然会抛出上述模块异常

<details>
<summary>英文:</summary>

I have this class initialiser:

System.setProperty("java.library.path", "C:\Users\lucas\Desktop\libraries");
System.loadLibrary("libTTARCHHelper");
TTARCH_LIBRARY=(TtarchLibrary)Native.loadLibrary(TtarchLibrary.class);


And the DLL is located at `C:\\Users\\lucas\\Desktop\\libraries\\libTTARCHHelper.dll`
If I run this in Eclipse I get &#39;The specified module could not be found&#39; and if I run it as a JAR I get &#39;no libTTARCHHelper in java.library.path&#39;.
How do I fix these? I even tried putting the DLL in a folder in the PATH environment variable.
Full debug using the file direct load:
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 &#39;C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll&#39;
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 &quot;main&quot; java.lang.UnsatisfiedLinkError: Unable to load library &#39;C:\Users\lucas\Desktop\libraries\libTTARCHHelper.dll&#39;:
The specified module could not be found.
The specified module could not be found.
The rest of the trace:
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.&lt;init&gt;(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.&lt;clinit&gt;(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
So it looks like it doesnt find it , It throws a 
java.lang.UnsatisfiedLinkError: The specified module could not be found.
I found the problem, the DLLs were for the wrong bit. Eclipse runs on 64 and the JAR uses the 32bit JVM. I downloaded the 32 bit dlls too and now it works when I run the JAR. However the 64 bit dlls in Eclipse keep throwing the module exception above
</details>
# 答案1
**得分**: 0
JNA会在`jna.library.path`环境变量指定的位置查找库。
一种选项是将其指定为Eclipse运行配置中的命令行选项。在Run菜单中,选择Run Configuration。转到(x)=Arguments选项卡。在VM arguments字段中添加以下内容:
`-Djna.library.path=C:\Users\lucas\Desktop\libraries`
您还可以在尝试加载DLL之前在代码中以编程方式设置它:
`System.setProperty("jna.library.path", "C:\\Users\\lucas\\Desktop\\libraries");`
正如您稍后在调试日志中所指出的,它可能会找到DLL,但无法打开它。可能导致这种情况的原因有:
- DLL在您的系统上对另一个DLL有运行时依赖性,而该DLL不在适当的路径中。常见的一个是Visual C++ Redistributable。
您可能需要查阅DLL文档以获取更多信息。您有时可以使用来自SysInternals库的[Process Monitor](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx)来尝试查看它正在打开的内容。
- 与上述情况相同的依赖性DLL的重复/不兼容版本
- 运行64位JVM,并尝试打开与64位不兼容的DLL,或者反之
- 您的Java程序可能没有权限读取库所在的目录
为了帮助诊断问题,请查看完整的堆栈跟踪。尽管最近的错误消息可能与问题无关(在“备份”搜索中找不到文件),但如果您查看堆栈跟踪,您可能会看到来自早期尝试打开DLL的多个被抑制异常,这些异常可能会为您提供更多信息且更具有信息量的调试信息。
<details>
<summary>英文:</summary>
JNA will look for libraries at locations in the `jna.library.path` environment variable.
One option is to specify it as a command line option in Eclipse&#39;s run configuration.  In the Run menu, select Run Configuration. Go to the (x)=Arguments tab.  Add this in the VM arguments field:
`-Djna.library.path=C:\Users\lucas\Desktop\libraries`
You can also programmatically set it in your code before attempting to load the DLL:
`System.setProperty(&quot;jna.library.path&quot;, &quot;C:\\Users\\lucas\\Desktop\\libraries&quot;);`
As you later indicated with the debug log, it may be finding the DLL but failing to open it.  Possible reasons for this can be:
- The DLL is runtime-dependent on another DLL on your system that&#39;s not in the appropriate path(s). A common one is the Visual C++ Distributable. 
You may need to consult the DLL documentation for more information. You can sometimes use [Process Monitor](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) from the SysInternals library to try to see what it&#39;s opening.
- Having a duplicate/incompatible version of a dependent DLL as above
- Running a 64-bit JVM and trying to open a DLL that&#39;s not 64-bit compatible, or vice-versa
- Your Java program may not have permission to read the directory where your library is located
To help diagnose the problem, look at the full stack trace.  While the most recent error message may not be relevant (file not found in a &quot;backup&quot; search) if you look down the trace you may see multiple suppressed exceptions from earlier attempts to open a DLL which may give you more informative debugging information.
</details>

huangapple
  • 本文由 发表于 2020年8月26日 00:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63583607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定