连续`System.loadLibrary` returns `UnsatisfiedLinkError`

Consecutive `System.loadLibrary` returns `UnsatisfiedLinkError`

我目前有两个库正在尝试通过 JNI 加载 - libA.solibB.so。来自 Oracle 的文档让我相信我可以简单地做到:

System.loadLibrary("A");
System.loadLibrary("B");

然后像这样编译和运行:

java -Djava.library.path="/path/to/libs" -jar myjarfile.jar

但是,这样做似乎只会加载第一个库,因为第二个库会抛出错误:

java.lang.UnsatisfiedLinkError: /path/to/libs/libB.so: libgomp.so.1: cannot open shared object file: No such file or directory

在验证两个库都在 java.library.path 指定的路径中后,我交换了语句,即:

System.loadLibrary("B");
System.loadLibrary("A");

并发现现在问题出在加载库 A:

java.lang.UnsatisfiedLinkError: /path/to/libs/libA.so: libgomp.so.1: cannot open shared object file: No such file or directory

让我相信无论出于何种原因我都无法连续拨打 loadLibrary 电话。执行 java -XshowSettings:properties 还可以确认我已将 java.library.path 正确设置为库的位置。

我尝试使用 System.load() 代替:

System.load("/path/to/libs/libA.so");
System.load("/path/to/libs/libB.so");

除了设置环境变量 LD_LIBRARY_PATH 但运气不好 - 这给了我同样的问题,第一个库被加载但第二个库抛出 java.lang.UnsatisfiedLinkError

我正在用头撞墙 - 有人有什么想法吗?

(注意:我认为它不相关,但我运行将我的代码放在基于 ubuntu 图像构建的 docker 容器中)

如果您完整阅读错误,您会注意到在这两种情况下它都指的是缺少 libgomp。您的共享对象(它们中的任何一个)很可能依赖于它。

确保 libgomp 已正确安装在您的平台上,然后重试。很确定它会解决问题。