2

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);
Bullseye
  • 23
  • 1
  • 8
  • You certainly don't have to write your own interface, however you can if you wish to use win32 or other (e.g. psapi) functions that are not pre-defined. I don't know why it's not working, I can only wildly guess that the embedded JNA dll cannot be linked at runtime. – Mark Jeronimus Nov 08 '18 at 23:09
  • The `load` method was introduced in JNA 5.0.0. Is it possible that you're using an old JNA version or that some old version is lingering somewhere which gets loaded and throws an exception because the method is not there? – cbr Nov 10 '18 at 11:16
  • Actually, you probably just have a new version of `jna-platform` but and old version of `jna`. Update your `jna` dependency. – cbr Nov 10 '18 at 11:17
  • Got something similar but with both libraries being 5.0.0 https://stackoverflow.com/questions/55982008/nosuchmethoderror-in-jna-platform – Alex May 04 '19 at 11:26

3 Answers3

3

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.

cbr
  • 12,563
  • 3
  • 38
  • 63
1

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>
  • indeed this worked for me. most recent version is still working, so it could be good to update your answer 5.6.0 – fvildoso Aug 31 '20 at 03:56
1

In my project, this error occurs when the following three dependency exist simultaneously. I move the tess4j dependency to the last and it works.

<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>
    
黄伟达
  • 11
  • 2