java 通过JNA从Java程序访问dll文件的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4805382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem in accessing dll file from a Java program through JNA
提问by Yatendra Goel
I have a dll file and I am trying to call functions of it through a Java program through JNA
我有一个 dll 文件,我试图通过JNA通过 Java 程序调用它的函数
But the problem is It is not able to locate my dll file and throwing the following exception:
但问题是它无法找到我的 dll 文件并抛出以下异常:
java.lang.UnsatisfiedLinkError: Unable to load library 'UsbDll': The specified module could not be found.
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:163)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:236)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:199)
at com.sun.jna.Native.register(Native.java:1018)
at com.MainClass.<clinit>(MainClass.java:15)
Exception in thread "main"
Below is my program:
下面是我的程序:
package com;
import com.sun.jna.Native
public class MainClass {
static {
Native.register("UsbDll");
}
public native int method();
public static void main(String[] args) {
}
}
The name of my dll file is UsbDll.dlland my operating system is Windows.
我的 dll 文件的名称是UsbDll.dll,我的操作系统是 Windows。
============================ EDITED ================================
============================ 编辑 ==================== ============
The location of my dll file is "c:\UsbDll.dll"
我的 dll 文件的位置是“c:\UsbDll.dll”
When I placed another dll file at the same location, JNA has located it so I think that the problem is with my "UsbDll.dll" file only.
当我在同一位置放置另一个 dll 文件时,JNA 已经找到了它,所以我认为问题仅在于我的“UsbDll.dll”文件。
When I tried to load both the dll files (UsbDll.dll and the another dll) with the following command
当我尝试使用以下命令加载两个 dll 文件(UsbDll.dll 和另一个 dll)时
System.load("c:\UsbDll.dll");
System.load("c:\another.dll");
It loaded the "another.dll" successfully but for "UsbDll.dll", it throws the following exception:
它成功加载了“another.dll”,但对于“UsbDll.dll”,它抛出以下异常:
java.lang.UnsatisfiedLinkError: C:\UsbDll.dll: Can't find dependent libraries
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1699)
at java.lang.Runtime.load0(Runtime.java:770)
at java.lang.System.load(System.java:1003)
at com.MainClass.<clinit>(MainClass.java:16)
Exception in thread "main"
Q1. It looks like it is not finding some dependent libraries. But as I am totally new to these dlls, Is there anything I am missing or I need to ask the vendor to provide me the dependent libraries.
一季度。看起来它没有找到一些依赖库。但是由于我对这些 dll 完全陌生,所以我是否遗漏了什么,或者我需要要求供应商为我提供依赖库。
OR
或者
Is it depends on some standard libraries which I can download from internet? If it is, then how to find the libraries name on which it depends?
它是否取决于我可以从互联网上下载的一些标准库?如果是,那么如何找到它所依赖的库名称?
============================ EDITED #2 ================================
========================== 编辑 #2 ==================== ==============
I ran the Dependency Walkeron my UsbDll.dll file to find the missing dependencies and found the following missing dependencies.
我在我的 UsbDll.dll 文件上运行Dependency Walker以查找丢失的依赖项并找到以下丢失的依赖项。
WER.DLL(referred by library WINNM.DLLfound in my c:\windows\system32)
WER.DLL(由在我的 c:\windows\system32 中找到的库WINNM.DLL引用)
IESHIMS.DLL(referred by library WINNM.DLLfound in my c:\windows\system32)
IESHIMS.DLL(由在我的 c:\windows\system32 中找到的库WINNM.DLL引用)
WDAPI920.DLL(referred directly by my UsbDll.dll)
WDAPI920.DLL(我的UsbDll.dll直接引用)
Q1. As WINNM.DLL was found in my system32 folder, it seems as the standard dll. And if this standard dll is referring to 2 missing dlls (WER.DLL & IESHIMS.DLL), then I suspect how other applications are working who are using this WINNM.DLL file?
一季度。由于 WINNM.DLL 是在我的 system32 文件夹中找到的,它似乎是标准的 dll。如果这个标准的 dll 指的是 2 个缺失的 dll(WER.DLL 和 IESHIMS.DLL),那么我怀疑使用这个 WINNM.DLL 文件的其他应用程序是如何工作的?
Q2. I googled for WDAPI920.dll (that was referred my UsbDll.dll directly) and many search results appeared with the same dll name. So it also looks like some standard library. So how to fix these dependencies? From where to download them? After downloading, Can I place them in the same directory in which my main dll (UsbDll.dll) is or I need to do something extra to load my dll (UsbDll.dll) sucessfully?
Q2。我在谷歌上搜索了 WDAPI920.dll(直接引用了我的 UsbDll.dll)并且许多搜索结果出现了相同的 dll 名称。所以它看起来也像一些标准库。那么如何修复这些依赖关系呢?从哪里下载它们?下载后,我可以将它们放在我的主 dll (UsbDll.dll) 所在的同一目录中,或者我需要做一些额外的事情才能成功加载我的 dll (UsbDll.dll)?
回答by Favonius
From your edited code it is quite evident that the UsbDll.dll
depends on some standard modules which are not there on your system (for example if it uses ATL and if you have don't have proper runtime then it is guaranteed to fail). To resolve this you will need proper runtime environment.
从您编辑的代码中可以明显看出,它UsbDll.dll
依赖于您系统上不存在的一些标准模块(例如,如果它使用 ATL,并且您没有适当的运行时,那么它肯定会失败)。要解决此问题,您将需要适当的运行时环境。
Alsoit is possible that the dll in concern depends on some vendor specific module. For you the best option is (and fastest option would be) to contact the vendor. Otherwise, try to install proper runtime from the microsoft site (but its more of hit-and-trial)
此外,所关注的 dll 也可能取决于某些供应商特定模块。对您来说,最好的选择是(最快的选择是)联系供应商。否则,尝试从 microsoft 站点安装适当的运行时(但它更多的是点击和试用)
Update更新
Use the below links for finding more about DLL dependency:
使用以下链接查找有关 DLL 依赖项的更多信息:
- How do I determine the dependencies of a .NET application?
- http://msdn.microsoft.com/en-us/library/ms235265.aspx
- Command line tool to find Dll dependencies
Update 2更新 2
See the below mentioned link for the missing dll details (but it is specific to the windows version)
有关缺少的 dll 详细信息,请参阅下面提到的链接(但它特定于 Windows 版本)
- Dependency Walker reports IESHIMS.DLL and WER.DLL missing?
- http://social.msdn.microsoft.com/Forums/en/vsx/thread/6bb7dcaf-6385-4d24-b2c3-ce7e3547e68b
- Dependency Walker 报告 IESHIMS.DLL 和 WER.DLL 丢失?
- http://social.msdn.microsoft.com/Forums/en/vsx/thread/6bb7dcaf-6385-4d24-b2c3-ce7e3547e68b
From few simple google queries, WDAPIXXX.dll
appears to be some win driver related thing (although i am not too sure). Check this link, they have something to say about WDAPI http://www.jungo.com/st/support/tech_docs/td131.html.
从几个简单的谷歌查询来看, WDAPIXXX.dll
似乎是一些与 win 驱动程序相关的东西(虽然我不太确定)。检查此链接,他们对 WDAPI http://www.jungo.com/st/support/tech_docs/td131.html有话要说。
回答by Daniel
The DLL must by in the Path specified by LD_LIBRARY_PATH (see your env), or, in the case of JNA, in the current directory.
DLL 必须位于 LD_LIBRARY_PATH 指定的路径中(请参阅您的环境),或者在 JNA 的情况下,位于当前目录中。