at the moment I'm trying to use JNA to set a non-Java application into focus and I've found the following code.
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
public class win32functions{
public static void setFocusToWindowsApp(String applicationTitle, int windowState) {
int state = windowState;
switch (state) {
default:
case 0:
state = User32.SW_SHOWNORMAL;
break;
case 1:
state = User32.SW_SHOWMAXIMIZED;
break;
case 2:
state = User32.SW_SHOWMINIMIZED;
break;
}
User32 user32 = User32.INSTANCE;
HWND hWnd = user32.FindWindow(null, applicationTitle);
if (user32.IsWindowVisible(hWnd)) {
if (state != User32.SW_SHOWMINIMIZED) {
user32.ShowWindow(hWnd, User32.SW_SHOWMINIMIZED);
}
user32.ShowWindow(hWnd, state);
user32.SetFocus(hWnd);
}
}
}
I also put the following dependencies into my pom.xml, the project is a fresh one with only a main file.
<properties>
<jna.version>5.5.0</jna.version>
</properties>
<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>${jna.version}</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>${jna.version}</version>
</dependency>
</dependencies>
This is our Programm entry point:
public class EntryPoint{
public static void main(String[] args) throws Exception
{
win32functions.setFocusToWindowsApp("Google Chrome", 0);
}
}
In theory, on compilation/run the Programm should just focus Google Chrome, which is opened while running.
However, Java gives the following error message:
Exception in thread "main" java.lang.NoSuchMethodError: 'com.sun.jna.Library com.sun.jna.Native.load(java.lang.String, java.lang.Class, java.util.Map)'
at com.sun.jna.platform.win32.User32.<clinit>(User32.java:49)
at win32functions.setFocusToWindowsApp(win32functions.java:21)
at EntryPoint.main(EntryPoint.java:16)
The only reference that I've found towards this problem was this NoSuchMethodError using JNA User32 platform map
It was almost the same error and the solution was to just change the version of JNA and JNA-platform. However, in my Maven Dependencies im using the same version for both platform and JNA. Can someone give me some insight? I'm actually really desperate, that's why I'm asking here.
The exception points to the same immediate error cause that your linked question had, specifically, your project can not find the JNA 5.x version of the Native
class (with the load(String, Class, Map)
method), suggesting you have an older version of the com.sun.jna.Native
class on your classpath.
It looks like you've already suspected this, and your pom.xml
excerpt looks correct in isolation, but you haven't given enough context to know whether there's another common error that's causing the wrong version to be loaded. One of the following actions should help solve your problem:
jna
and jna-platform
dependencies first in the highest level pom.xml
file in your project. If you have any other dependencies which transitively load an earlier version of JNA, they may be processing them first.pom.xml
file.
mvn install
or mvn package
should accomplish this.Maven > Update Project
.Native.jar
. Spring Boot is a common culprit here.