将目录添加到 java.library.path 后 System.loadLibrary() 上出现 UnsatisfiedLinkError

UnsatisfiedLinkError on System.loadLibrary() after adding the directory to java.library.path

我想在 Ubuntu 19.10.

我的 Java IntelliJ Maven 项目中加载本地库 /opt/gurobi902/linux64/lib/libGurobiJni90.so

我的第一次尝试是像这样添加环境变量 LD_LIBRARY_PATH:export LD_LIBRARY_PATH="/opt/gurobi902/linux64/lib"(我知道我已经完全覆盖它而不是附加到它,因为它之前根本没有设置。)

我认为它有效,因为当我 运行 终端命令 java -XshowSettings:propertiesjava -XshowSettings:properties 它 returns 这个(除其他外):

java.library.path = /opt/gurobi902/linux64/lib
        /usr/java/packages/lib
        /usr/lib/x86_64-linux-gnu/jni
        /lib/x86_64-linux-gnu
        /usr/lib/x86_64-linux-gnu
        /usr/lib/jni
        /lib
        /usr/lib

BUT 当我想使用 System.loadLibrary("GurobiJni90") 在我的项目中加载库时,我收到此异常:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no GurobiJni90 in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]

如您所见,我之前添加的目录没有在异常语句的java.library.path中列出。

顺便说一句。这是我的 pom.xml 图书馆条目:

<dependency>
    <groupId>com.gurobi</groupId>
    <artifactId>gurobi</artifactId>
    <version>9.0.2</version>
    <scope>system</scope>
    <systemPath>/opt/gurobi902/linux64/lib/gurobi.jar</systemPath>
</dependency>

环境变量LD_LIBRARY_PATH需要指向一个目录(或其列表),而不是特定的库。 Gurobi installation guide.

中也对此进行了概述

问题现已解决!

我的 linux 发行版 (Ubuntu 19.10) 似乎不允许应用程序读取设置了环境变量的 .bashrc 文件。这解释了为什么 java.library.path 从终端或我自己的 IntelliJ 项目访问时保存不同的内容。

Gurobi Installation Guide提到了这个问题(但我认为我没有受到影响):

In some Linux distributions, applications launched from the Linux desktop won't read .bashrc (or .cshrc). You may need to set the Gurobi environment variables in .bash_profile or .profile instead. Unfortunately, the details of where to set these variables vary widely among different Linux distributions. We suggest that you consult the documentation for your distribution if you run into trouble.

我已经通过菜单 [运行] > [编辑配置] > [环境变量] 手动将环境变量添加到我的 IntelliJ 项目中,因为我只在这个项目中需要它们。适合我!