1

I want to use JNA to detect foreground application on Linux (Ubuntu 14). I followed this link Find out what application (window) is in focus in Java but I got the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'XLib': Native library (linux-x86-64/libXLib.so) not found in resource path ([file:/home/zzhou/workspace/home_prioritization_plus/bin/, file:/home/zzhou/Downloads/jna-4.1.0.jar, file:/home/zzhou/Downloads/jna-platform-4.1.0.jar])
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:271)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
    at com.sun.jna.Library$Handler.<init>(Library.java:147)
    at com.sun.jna.Native.loadLibrary(Native.java:412)
    at com.sun.jna.Native.loadLibrary(Native.java:391)
    at FunctionalityTest$XLib.<clinit>(FunctionalityTest.java:15)
    at FunctionalityTest.main(FunctionalityTest.java:23)

The code is:

import com.sun.jna.Native;
import com.sun.jna.Platform;
import com.sun.jna.Pointer;
import com.sun.jna.platform.unix.X11;
import com.sun.jna.win32.StdCallLibrary;

public class FunctionalityTest {

    static class Psapi {
        static { Native.register("psapi"); }
        public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
    }

    public interface XLib extends StdCallLibrary {
        XLib INSTANCE = (XLib) Native.loadLibrary("XLib", Psapi.class); // <-- PROBLEM

        int XGetInputFocus(X11.Display display, X11.Window focus_return, Pointer revert_to_return);
    }

    public static void main(String args[]) {
        if(Platform.isLinux()) {  // Possibly most of the Unix systems will work here too, e.g. FreeBSD
            final X11 x11 = X11.INSTANCE;
            final XLib xlib= XLib.INSTANCE;
            X11.Display display = x11.XOpenDisplay(null);
            X11.Window window=new X11.Window();
            xlib.XGetInputFocus(display, window,Pointer.NULL);
            X11.XTextProperty name=new X11.XTextProperty();
            x11.XGetWMName(display, window, name);
            System.out.println(name.toString());
        }
    }

}

To import JNA library, I downloaded jna and jna-platform from https://github.com/twall/jna and use Configure Build Path... in Eclipse to add library. I did not install anything. May that be the source of the problem?

Thanks for your help.

Community
  • 1
  • 1
zyl1024
  • 690
  • 1
  • 9
  • 23
  • 1
    is XLib your library? Or is your code expecting it to be available on the system (might need to install it then). Most libs on most linux distro's are located either in `/lib` or `/usr/lib`. (also com.sun.jna.win32.* is for the Windows OS). – SnakeDoc Sep 22 '14 at 16:29
  • Possible duplicate of [Load Native Library from Class path](http://stackoverflow.com/questions/23189776/load-native-library-from-class-path). – jww Sep 22 '14 at 17:02

1 Answers1

1

Afaik, even for JNA, you have to load the library in Java in order for JNA to find it. (tested for win32, not linux)

Try this just above Native.loadLibrary:

System.loadLibrary("XLib");
Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50