未找到JNA库和本机库错误

时间:2014-09-22 16:24:44

标签: java linux jna

我想使用JNA来检测Linux上的前台应用程序(Ubuntu 14)。我按照这个链接 Find out what application (window) is in focus in Java 但是我收到了以下错误:

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)

代码是:

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

}

要导入JNA库,我从https://github.com/twall/jna下载了jna和jna-platform,并在Eclipse中使用Configure Build Path ...添加库。我没有安装任何东西。可能是问题的根源?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

Afaik,即使对于JNA,你也必须用Java加载库,以便JNA找到它。 (测试win32,而不是linux)

Native.loadLibrary上面试试这个:

System.loadLibrary("XLib");