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.