我试图在Ubuntu上运行的JavaFX应用程序中使用JNA检索当前的焦点窗口。
为了实现我的目标,我使用X11 api,更准确地说是使用XGetInputFocus
函数:
if (Platform.isLinux()) {
System.err.println("Linux platform");
final X11 x11 = X11.INSTANCE;
final XLib xlib = XLib.INSTANCE;
X11.Display display = x11.XOpenDisplay(null);
//X11.Window window = new X11.Window();
X11.WindowByReference winRef = new X11.WindowByReference();
Pointer pointer = Pointer.NULL;
xlib.XGetInputFocus(display, winRef, pointer);
/*X11.XTextProperty name = new X11.XTextProperty();
x11.XGetWMName(display, window, name);
System.out.println(name.toString());*/
}
-
public interface XLib extends X11 {
XLib INSTANCE = (XLib) Native.loadLibrary("X11", XLib.class);
//void XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
void XGetInputFocus(X11.Display display, X11.WindowByReference focus_return, Pointer revert_to_return);
}
不幸的是,我遇到了以下异常,我尝试X11.WindowByReference
并且X11.Window
有参数,但它没有任何变化,异常是相同的。
Linux platform
Looking in classpath from sun.misc.Launcher$AppClassLoader@73d16e93 for /com/sun/jna/linux-x86-64/libjnidispatch.so
Found library resource at jar:file:/home/puglic/eclipse/jna-4.2.2.jar!/com/sun/jna/linux-x86-64/libjnidispatch.so
Looking for library 'X11'
Adding paths from jna.library.path: null
Trying libX11.so
Found library 'X11' at libX11.so
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f4063178e47, pid=21693, tid=0x00007f405423a700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_111-b14) (build 1.8.0_111-b14)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libX11.so.6+0x22e47] XGetInputFocus+0x57
此外,它会生成一个名为hs_err_pid21693.log
的巨大错误文件。在哪里我们可以找到这种线:
...
Internal exceptions (10 events):
Event: 0,223 Thread 0x00007f213000a000 Exception <a 'java/lang/NoSuchFieldError': method resolution failed> (0x00000000d7b0ded8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/methodHandles.cpp, line 1146]
Event: 0,262 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b98008) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,262 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b98218) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b9a7e8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/security/PrivilegedActionException'> (0x00000000d7b9a9f8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1386]
Event: 0,263 Thread 0x00007f213000a000 Exception <a 'java/lang/NullPointerException'> (0x00000000d7b9afd0) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/interpreter/linkResolver.cpp, line 1178]
Event: 0,621 Thread 0x00007f21302c1800 Exception <a 'java/lang/NullPointerException'> (0x00000000d8349f68) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/interpreter/linkResolver.cpp, line 1178]
Event: 0,621 Thread 0x00007f21302c1800 Exception <a 'java/lang/NullPointerException'> (0x00000000d8349f68) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jvm.cpp, line 1390]
Event: 0,871 Thread 0x00007f21302c1800 Exception <a 'java/io/FileNotFoundException'> (0x00000000d87dc6d8) thrown at [/HUDSON3/workspace/8-2-build-linux-amd64/jdk8u111/7883/hotspot/src/share/vm/prims/jni.cpp, line 709]
Event: 1,041 Thread 0x00007f21302c1800 Implicit null exception at 0x00007f212120d176 to 0x00007f212120d531
...
答案 0 :(得分:0)
我不认为XGetInputFocus
为您提供了传递NULL作为最终参数的选项。您需要传入一个有效的内存位置的地址,函数可以在其中写入revert_to
&#34; return&#34;值。
您应该使用非null IntByReference
对象而不是NULL。