Why do I get java.lang.UnsatisfiedLinkError: Unabl

2019-07-09 01:44发布

问题:

I am trying to call a .dll file from my Java app using JNA. I am getting the following exception:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'C:\Windows\System32\foo.dll': The specified module could not be found.

Both the .dll and my jdk are 32 bit (OS_ARCH="i586") although I am running it on a 64 bit Windows 7 PC.
The .dll is in the System32 folder.

I am using Eclipse and have added the System32 folder to the Native library location in the JRE System library under the Libraries tab in the Java Build Path Properties (although I don't think this should be necessary).

I would be grateful for any help or suggestions.
Thanks,
-Munk.

回答1:

This is down to the file system redirector. You are executing under the WOW64 emulator that emulates 32 bit Windows on a 64 bit system. Under WOW64, the system32 is redirected to SysWOW64. You'll need to put your DLL there.

With that said, the system directory is owned by, and private to, the system. You are expected not to put DLLs into the system directory. You should find a way to put your DLL in some other location.



回答2:

Another reason for error is that the dll is only supported for 32 version not for 64 bit OS. You have to confirm the dll vendor.



回答3:

my os is windows-x64 and my jdk is x64.So the problem is same with you. my solution is you must install jdk-x86,and put you dll into the jdk-x86/bin directory.



回答4:

Three possible reasons for this issue, if the dll file is not broken:

  1. 32 bit 64 bit Compatibility. 32bit dll can only be running on a 32bit jdk or jre. By using Cygwin command file <filename> we can tell if a dll is 32bit or 64bit.

  2. the dll is not lacated in the right path, so java cannot locate it. Generally speaking we can use some absolut path other than System32 to ensure that the path is right.

  3. the dll that we are loading is requires other dlls.

How can we handle the 3rd possibility:

  1. using JNI's System.loadLibrary() mthod can give me more hint, compared with JNA. It may says something like: Exception in thread "main" java.lang.UnsatisfiedLinkError: MyLibrary.dll: Can't find dependent libraries. It means some libraries MyLibrary.dll depends is missing. By using dependency walker we can tell what dlls are needed.

  2. By loading these dlls before the dll that we want to load, we can solve this issue.



标签: java windows jna