问题
I received the following error on the first attempt of using the User32.Instance:
Exception in thread "main" java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;Ljava/lang/Class;Ljava/util/Map;)Lcom/sun/jna/Library;
at com.sun.jna.platform.win32.User32.(User32.java:48)
whilst trying to run a JNA pre-defined mapping of the Windows User32 class functions.
I tried running the following code:
HWND hwnd = User32.INSTANCE.FindWindow(null,"new 2 - Notepad++");
User32.INSTANCE.SetForegroundWindow(hwnd);
Do I have to declare my own Interface or am I able to use the User32 JNA mapping located in jna-platform? What am i doing wrong?
Edit: The error is on this line from the com.sun.jna.platform.win32.user32:
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
回答1:
I was able to reproduce this bug by compiling against an old jna
package (pre-5.0.0) and a new jna-platform
package (5.0.0):
Exception in thread "main" java.lang.NoSuchMethodError: com.sun.jna.Native.load(Ljava/lang/String;Ljava/lang/Class;Ljava/util/Map;)Lcom/sun/jna/Library;
at com.sun.jna.platform.win32.User32.<clinit>(User32.java:48)
at sandboxjava.Main.main(Main.java:8)
The issue is that JNA deprecated the Native.loadLibrary
method in version 5.0.0 and introduced the Native.load
method. The newer jna-platform
package uses the new method, but because the jna
package is an older version, the load
method simply does not exist in the package.
You should either upgrade the jna
package to 5.0.0 (latest at the time of writing), or downgrade jna-platform
to a pre-5.0.0 version.
回答2:
Update the maven using below dependency, It worked for me.
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
来源:https://stackoverflow.com/questions/53217390/nosuchmethoderror-using-jna-user32-platform-map