jar calls the dll file and prompts that the specified module cannot be found. Unable to load library

Posted by jlgray48 on Thu, 27 Jan 2022 13:18:10 +0100

catalogue

Problem description

Solution 1

Solution 2

effect

Supplementary notes

Reference link

Problem description

On the Windows system, the Swing project developed by myself needs to use JNA to call the dll file of the dynamic link library( Java simple system monitoring )After using Maven assembly plugin (jar with dependencies) to form a jar package, the machine can run normally, but it can't run on another computer. Prompt: Java lang.UnsatisfiedLinkError: Unable to load library 'BatteryMonitor':
The specified module cannot be found.

However, batterymonitor The dll file is clearly in front of you, and even if you change to an absolute path, you will report an error. Moreover, you can't manually insert the dll file into the win32-x86-64 folder of JNA through Java commands. (desperate, the furthest distance in the world is that I'm in front of you, but you can't see...)

Complete error reporting information:

java.lang.UnsatisfiedLinkError: Unable to load library 'BatteryMonitor':
The specified module cannot be found.

The specified module cannot be found.

The specified module cannot be found.

Can't obtain InputStream for win32-x86-64/BatteryMonitor.dll
	at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:302)
	at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:455)
	at com.sun.jna.Library$Handler.<init>(Library.java:192)
	at com.sun.jna.Native.load(Native.java:596)
	at com.sun.jna.Native.load(Native.java:570)
	at cxzgwing.dll.Dll.<clinit>(Dll.java:7)
	at cxzgwing.utils.AppUtil.getBatteryPercent(AppUtil.java:37)
	at cxzgwing.Window.initBatteryJLabel(Window.java:130)
	at cxzgwing.Window.<init>(Window.java:62)
	at cxzgwing.App.main(App.java:8)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:85)
	at com.exe4j.runtime.WinLauncher.main(WinLauncher.java:94)
	Suppressed: java.lang.UnsatisfiedLinkError: The specified module cannot be found.

		at com.sun.jna.Native.open(Native Method)
		at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
		... 15 more
	Suppressed: java.lang.UnsatisfiedLinkError: The specified module cannot be found.

		at com.sun.jna.Native.open(Native Method)
		at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
		... 15 more
	Suppressed: java.lang.UnsatisfiedLinkError: The specified module cannot be found.

		at com.sun.jna.Native.open(Native Method)
		at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:265)
		... 15 more
	Suppressed: java.io.IOException: Can't obtain InputStream for win32-x86-64/BatteryMonitor.dll
		at com.sun.jna.Native.extractFromResourcePath(Native.java:1115)
		at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:276)
		... 15 more

 

Solution 1

Refer to the JNA documentation on Github Setting up a Windows Development Environment , there are four points in Prerequisites, each of which involves an environment or program that needs to be installed.

 

Solution 2

Direct installation Visual Studio , check "desktop development using C + + and" general Windows platform development ", and then install it. After installation, it's OK. (guess: these two modules contain the environment required by JNA)

The above figure shows that when I installed Visual Studio Community 2022, I only checked "general Windows platform development" at the beginning, found that there was still an error after running the program, then clicked the "modify" button in the installation interface, and then checked "desktop development using C + +". In fact, you can check these two modules at the beginning of installation. (separate installation is to test the modules that need to be installed at least)

effect

After installing VS2022, I can run my system monitoring again.

Supplementary notes

Because I wrote this dll file myself( Java call dll file ), VS2022 Community is also installed on this machine, so this machine can run. However, after sending it to my friends, I found that it can't run. Then I also installed win10 system on the virtual machine, and I found the same problem when running in a clean win10 environment. Then I found all kinds of materials on the Internet and didn't solve them. Finally, I thought, I went directly to the official documents to see if there was any inspiration. As a result, I found this treasure tutorial (nanny tutorial).

After a closer look, I find that the first point in Prerequisites is to Install "Visual Studio Community 2019" or the "Build Tools for Visual Studio 2019". Then I think about it carefully. It seems that I have installed visual studio before (to write dll myself). That should be the environment required for the installation of JNA, So I personally tested the solution 2 in the virtual machine and summarized this experience.

The content in solution 1 has not been tested in person yet. It is an official document. The problem should not be big. Because of the trouble, I didn't go to the installation environment. I started Visual Studio directly.

Reference link

[1] java-native-access.Setting up a Windows Development Environment.2021-01-18 04:13
https://github.com/java-native-access/jna/blob/master/www/WindowsDevelopmentEnvironment.md

Topics: Java jar dll