[英]NoSuchMethodError using JNA User32 platform map
I received the following error on the first attempt of using the User32.Instance:我在第一次尝试使用 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;
线程“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)在 com.sun.jna.platform.win32.User32.(User32.java:48)
whilst trying to run a JNA pre-defined mapping of the Windows User32 class functions.同时尝试运行 Windows User32 类函数的 JNA 预定义映射。
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?我必须声明我自己的接口还是我可以使用位于 jna-platform 中的 User32 JNA 映射? What am i doing wrong?
我究竟做错了什么?
Edit: The error is on this line from the com.sun.jna.platform.win32.user32:编辑:错误来自 com.sun.jna.platform.win32.user32 的这一行:
User32 INSTANCE = Native.load("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
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):通过针对旧的
jna
包(5.0.0 之前)和新的jna-platform
包(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.问题是 JNA 在 5.0.0 版本中弃用了
Native.loadLibrary
方法并引入了Native.load
方法。 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.较新的
jna-platform
包使用了新方法,但由于jna
包是旧版本,因此包中根本不存在load
方法。
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.您应该将
jna
包升级到 5.0.0(撰写本文时的最新版本),或者将jna-platform
降级到 5.0.0 之前的版本。
Update the maven using below dependency, It worked for me.使用以下依赖项更新 Maven,它对我有用。
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.1</version>
</dependency>
In my project, this error occurs when the following three dependency exist simultaneously.在我的项目中,当以下三个依赖同时存在时,就会出现这个错误。 I move the tess4j dependency to the last and it works.
我将 tess4j 依赖移到最后一个,它可以工作。
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>4.4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}\src\resources\lib\opencv\jna-
4.4.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun.jna.paltform</groupId>
<artifactId>paltform</artifactId>
<version>4.4.0</version>
<scope>system</scope>
<systemPath>${project.basedir}\src\resources\lib\opencv\jna-platform-4.4.0.jar</systemPath>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.