显式声明 lib 时出现 UnsatisfiedLinkError 异常

UnsatisfiedLinkError exception when lib is explicitly declared

当找不到 libmysqlclient.so.18 时,尝试执行我的 Java 应用程序会产生 UnsatisfiedLinkError 异常,即使它是在 LD_LIBRARY_PATH-Djava.library.path/etc/ld.[= 中明确声明130=]

问题描述

我正在尝试使用 pcap4j (https://github.com/kaitoy/pcap4j),一个用于 libpcap 的 Java 包装器,因此我可以从 Java 应用程序嗅探我机器 NIF 上的数据包。 由于 libpcap 需要超级用户权限才能执行此任务,因此我必须以某种方式为执行此应用程序的非特权用户提供访问 NIF 的能力。

pcap4j 的维护者建议向 java 命令授予能力 CAP_NET_RAWCAP_NET_ADMIN,如下所示: setcap cap_net_raw,cap_net_admin=eip /path/to/java

由于实施限制,我受到以下限制:

CAP_NET_RAWCAP_NET_ADMIN权限授予java命令后,问题出现了。执行我的应用程序时出现以下异常:

Error creating entity
java.lang.UnsatisfiedLinkError: /path/to/app/lib/libxpherejava.so: libmysqlclient.so.18: cannot open shared object file: No such file or directory
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
        at java.lang.Runtime.loadLibrary0(Runtime.java:870)
        at java.lang.System.loadLibrary(System.java:1122)

详情

JAVA: java-1.8.0-openjdk-1.8.0.171-8

OS: Linux user-me 3.10.0-862.6.3.el7.x86_64 #1 SMP 6 月 15 日星期五17:57:37 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux [Red Hat Enterprise Linux Server release 7.5 (Maipo)]

LD_LIBRARY_PATH 包含未找到库的显式路径:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/lwp:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre/lib/amd64/jli/libjli.so:/usr/lib64/mysql/libmysqlclient.so.18
export LD_LIBRARY_PATH

LD_LIBRARY_PATH 通过使用 java.library.path:

传递给 JVM
exec java
-XshowSettings:properties 
-Djava.library.path=${LD_LIBRARY_PATH}
-d64
...

"-XshowSettings:properties" 在执行 java 命令时为我提供了以下输出:

java.library.path =
        /path/to/app/lib
        /path/to/app/lib/glib-2.0
        /usr/lib/lwp
        /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre/lib/amd64/jli/libjli.so
        /usr/lib64/mysql/libmysqlclient.so.18

其中 /usr/lib64/mysql/libmysqlclient.so.18 是指向 /usr/lib64/mysql/libmysqlclient.so.18.0.0

的符号链接

似乎是 JVM(或任何实体请求从 libxpherejava.so 访问 libmysqlclient.so.18)找不到 libmysqlclient.so.18,即使它的路径明确提供给 java.library.path 和文件确实存在。

With LD_LIBRARY_PATH 包含到 libmysqlclient.so.18 (/usr/lib64/mysql/libmysqlclient.so.18) 的路径,发出 ldd of libxpherejava.so 产生 libmysqlclient.so.18 无法找到

[user@user-me log]$ ldd /path/to/app/lib/libxpherejava.so
        linux-vdso.so.1 =>  (0x00007ffe1a73d000)
        libcrypto.so.10 => /lib64/libcrypto.so.10 (0x00007fd727df4000)
        libssl.so.10 => /lib64/libssl.so.10 (0x00007fd727b83000)
        libxphereS.so => /path/to/app/lib/libxphereS.so (0x00007fd727973000)
        libmysqlclient.so.18 => not found
        libz.so.1 => /lib64/libz.so.1 (0x00007fd72775d000)
        libnsl.so.1 => /lib64/libnsl.so.1 (0x00007fd727543000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd727327000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fd727025000)
        libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007fd726d11000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd726944000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fd726740000)
        libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fd7264f3000)
        libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fd72620b000)
        libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fd726007000)
        libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fd725dd4000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd728718000)
        libpcre.so.1 => /lib64/libpcre.so.1 (0x00007fd725b72000)
        libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fd725964000)
        libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fd725760000)
        libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fd725547000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fd725320000)

这是ld的内容。so.conf:

[user@user-me lib]$ cat /etc/ld.so.conf
/path/to/app/lib/
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre/lib/amd64/jli/libjli.so
/usr/lib64/mysql/libmysqlclient.so.18

两个 lib 都是 64 位的 已编译:

[user@user-me lib]$ file libxpherejava.so
libxpherejava.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=82e1c673e732eb2d3770883b14facf3eff091243, not stripped
[user@user-me lib]$ file /usr/lib64/mysql/libmysqlclient.so.18
/usr/lib64/mysql/libmysqlclient.so.18: symbolic link to libmysqlclient.so.18.0.0'
[user@user-me lib]$ file /usr/lib64/mysql/libmysqlclient.so.18.0.0
/usr/lib64/mysql/libmysqlclient.so.18.0.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=79978c5f4fb259a5a146614e260ea0720dd31d3b, stripped

A strace 在执行 "exec java" 命令的脚本上产生这个 --> https://dumpz.org/aGHQpNk9Znmk

相关问题

有谁知道为什么找不到libmysqlclient.so.18

/usr/lib64 下创建指向有问题的库 libmysqlclient.so.18 的符号链接可以解决问题。异常消失了。