JNA – JVM Fatal Error – XGetInputFocus – Ubuntu

OS: Ubuntu 16.04
JNA: 4.2.2
JDK: 1.8.0u111

I’m trying to retrieve the current focused window using JNA in a JavaFX application running on Ubuntu.

To achieve my goal, I’m using X11 api, more precisely the function XGetInputFocus in that way :

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);
}

Unfortunately, I got the following exception, I tried X11.WindowByReference and X11.Window has parameter, but it changes nothing, the exception is the same.

Linux platform
Looking in classpath from [email protected] 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

Also, It generates a huge error file named hs_err_pid21693.log. Where we can find this kind of line :

...
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
...

Answer

I don’t think XGetInputFocus gives you the option of passing in NULL as the final argument. You need to pass in the address of a valid memory location where the function can write its revert_to “return” value.

You should use a non-null IntByReference object rather than NULL.

Leave a Reply

Your email address will not be published. Required fields are marked *