简体   繁体   English

NoSuchMethodError 使用 JNA User32 平台映射

[英]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.

相关问题 JNA 4.1.0中User32的类位置 - Class location for User32 in JNA 4.1.0 jna平台中的NoSuchMethodError - NoSuchMethodError in jna-platform Java中带有UTF-8的User32 GetWindowTextA? - User32 GetWindowTextA with UTF-8 in Java? 使用来自 user32.dll 的 SendMessage 和 java 中的 jna - 错误 - using SendMessage from user32.dll with jna in java - error JNA:com.sun.jna.platform.win32.Win32Exception-访问被拒绝 - JNA:com.sun.jna.platform.win32.Win32Exception- access denied 如何只获取窗口的可见部分(Windows,gdi32,user32等) - How to get only the visible part of a window (Windows, gdi32, user32, etc) CreateProcessAsUser 方法中的 hToken (com.sun.jna.platform.win32.WinNT.HANDLE) 是什么。 如何使用 windowsIdentity 对象检索它 - What is hToken (com.sun.jna.platform.win32.WinNT.HANDLE) in CreateProcessAsUser method. How to retrieve it using windowsIdentity Object com.sun.jna.Pointer无法转换为com.sun.jna.platform.win32.WinDef.LPARAM - com.sun.jna.Pointer cannot be cast to com.sun.jna.platform.win32.WinDef.LPARAM 如何在Java中传递宽字符串,以在user32 lib中调用MessageBoxW函数 - How to pass wide strings in Java calling the MessageBoxW function in user32 lib 使用 JNA 时 Mac 和 Windows 之间的 FTDI 库平台差异 - FTDI library platform differences between Mac and Windows when using JNA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM