简体   繁体   中英

Problem in accessing dll file from a Java program through JNA

I have a dll file and I am trying to call functions of it through a Java program through JNA

But the problem is It is not able to locate my dll file and throwing the following exception:

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 and my operating system is Windows. ,我的操作系统是Windows。

============================ EDITED ================================

The location of my dll file is "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.

When I tried to load both the dll files (UsbDll.dll and the another dll) with the following command

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:

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.

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 ================================

I ran the Dependency Walker on my UsbDll.dll file to find the missing dependencies and found the following missing dependencies.

(referred by library found in my c:\\windows\\system32) (由我的c:\\ windows \\ system32中的库引用)

(referred by library found in my c:\\windows\\system32) (由我的c:\\ windows \\ system32中的库引用)

(referred directly by my UsbDll.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?

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?

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.

Also it 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)


Update

Use the below links for finding more about DLL dependency:

  1. How do I determine the dependencies of a .NET application?
  2. http://msdn.microsoft.com/en-us/library/ms235265.aspx
  3. Command line tool to find Dll dependencies


Update 2

See the below mentioned link for the missing dll details (but it is specific to the windows version)

  1. Dependency Walker reports IESHIMS.DLL and WER.DLL missing?
  2. 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 .

DLL必须在LD_LIBRARY_PATH指定的Path中(参见您的env),或者在JNA的情况下,在当前目录中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM