简体   繁体   English

找不到JNA库和本机库错误

[英]JNA library and native library not found error

I want to use JNA to detect foreground application on Linux (Ubuntu 14). 我想使用JNA在Linux(Ubuntu 14)上检测前台应用程序。 I followed this link Find out what application (window) is in focus in Java but I got the following error: 点击了此链接, 找出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)

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. 要导入JNA库,我从https://github.com/twall/jna下载了jna和jna-platform,并在Eclipse中使用Configure Build Path ...添加库。 I did not install anything. 我没有安装任何东西。 May that be the source of the problem? 这可能是问题的根源吗?

Thanks for your help. 谢谢你的帮助。

Afaik, even for JNA, you have to load the library in Java in order for JNA to find it. Afaik,即使对于JNA,也必须以Java加载库才能让JNA找到它。 (tested for win32, not linux) (已针对Win32(而非Linux)进行了测试)

Try this just above Native.loadLibrary : Native.loadLibrary上方尝试以下Native.loadLibrary

System.loadLibrary("XLib");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到OSGI JNA本机库pcap - OSGI JNA native library pcap not found 尝试在Mac OS X上加载JNA库时出现“无法加载库:在资源路径中找不到JNA本机支持” - “Unable to load library: JNA native support not found in resource path” on trying to load JNA library on Mac OS X 找不到JNA运行时相关的库 - JNA runtime dependent library not found 在Node中加载Java JNA本机库 - Load Java JNA native library in Node Windows,JNA:无法加载本机库-UnsatisfiedLinkError - Windows, JNA: Cannot load native library - UnsatisfiedLinkError JNA:本机库依赖项和JAR提取 - JNA: Native Library dependencies and JAR extraction 将 JNA Java 映射到本机 C 共享库 - Mapping JNA Java to Native C Shared Library 在资源路径中找不到本机库(com / sun / jna / android-aarch64 / libjnidispatch.so) - Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path 在 MacOS 上的 AdoptOpenJDK 上链接 JNA 库时出错 - Error linking JNA library on AdoptOpenJDK on MacOS UnsatisfiedLinkError:在资源路径 (.) 中找不到本机库 (com/sun/jna/android-x86-64/libjnidispatch.so) - UnsatisfiedLinkError: Native library (com/sun/jna/android-x86-64/libjnidispatch.so) not found in resource path (.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM