如何在 Linux 中将 .so 文件添加到 java.library.path

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16227045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:48:15  来源:igfitidea点击:

How to add .so file to the java.library.path in Linux

linuxjava.library.path

提问by Lihkinisak

I am working with a java application which needs a .dllfile in java.library.path in windows. To run same application in Linux I have the respective .sofile which should be added to java.library.pathin linux machine, I didnt find any easy solution for this so far

我正在使用一个 java 应用程序,它需要.dll在 windows 中的 java.library.path 中的一个文件。要在 Linux 中运行相同的应用程序,我有相应的.so文件应该添加到java.library.pathlinux 机器中,到目前为止我没有找到任何简单的解决方案

I did put the .soin a folder which is already in the class path, but my application still complains there is no required .sofile in java.library.path

我确实把它.so放在一个已经在类路径中的文件夹中,但我的应用程序仍然抱怨没有必需的.so文件java.library.path

I'd like to find:

我想找到:

  1. Ways to add .soto java.library.path
  2. How to know if its already added (when added)
  1. 添加.so方法java.library.path
  2. 如何知道它是否已经添加(添加时)

采纳答案by Jose Luis Martin

Add the containing directory to LD_LIBRARY_PATHbefore launching the application

LD_LIBRARY_PATH在启动应用程序之前将包含目录添加到

        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/pathOfContainingDirectory

Use java -XshowSettings:propertiesto show the java.library.path (and others) value.

使用java -XshowSettings:properties来显示的java.library.path(及其他)值。

回答by Ravindu

I used java -XshowSettings:properties method and found the path of a previously set folder and copied my so file to that folder

我使用 java -XshowSettings:properties 方法并找到了先前设置的文件夹的路径并将我的 so 文件复制到该文件夹

回答by Ardika Rommy Sanjaya

File file = null;
private String[] libs_path = System.getProperty("java.library.path").split(":");
public boolean isInstalled() {
    boolean isInstalled = false;
    for(String lib : libs_path) {
            file = new File(lib+"/"+"yourlib.so");
            if(file.exists()) {
                isInstalled = true;
                break;
            }
        }
    return isInstalled;
}


public void install() {
    if(!isInstalled) {
        for(String lib: lib_path) {
             // copy your .so to lib
             break;
        }
    }
}

回答by teardrop

I had a lot of trouble figuring this out, please make sure that you have libprefix in the library name.

我在弄清楚这一点时遇到了很多麻烦,请确保您lib在库名称中有前缀。

So steps,

所以步骤,

  1. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/some/pathOfContainingDirectory"

  2. Rename libraries to have lib as a prefix. [Add this as part of build script]

    mv JNIDemo.so libJNIDemo.so
    
  1. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/some/pathOfContainingDirectory"

  2. 重命名库以将 lib 作为前缀。[将此添加为构建脚本的一部分]

    mv JNIDemo.so libJNIDemo.so
    

Check this answer for detailed explanation https://stackoverflow.com/a/3987567/2076566

检查此答案以获取详细说明https://stackoverflow.com/a/3987567/2076566

回答by Chris

I believe this to be a better way to do it as it only needs one single link to be created, no file edits needed, no exports or running of source before an application is run. And no bashrc lines needed.

我相信这是一种更好的方法,因为它只需要创建一个链接,不需要文件编辑,在应用程序运行之前不需要导出或运行源代码。并且不需要 bashrc 行。

That said, if you upgrade it may cause unintentional errors to future java releases as it might load an outdated library, although one link removal also solves this.

也就是说,如果您升级它可能会导致未来的 Java 版本出现意外错误,因为它可能会加载过时的库,尽管删除链接也可以解决这个问题。

This works under ubuntu 18.04 with java 11 opensdk installed.

这适用于安装了 java 11 opensdk 的 ubuntu 18.04。

$ locate libjawt.so
$ java -XshowSettings:properties

From the two commands above, you can find the two paths you need to adjust this to your own system.

从上面的两个命令中,你可以找到你需要调整到你自己的系统的两条路径。

sudo ln -s /path/from/locate/libjawt.so /path/from/properties/libawt.so

I made a symlink for java ll open sdk to find the library and mine was as follows because /usr/lib is one of the paths in the output. There is (in my output) a java path you may choose to use instead of /usr/lib if you want to keep java library (even symlinks) together.

我为 java ll open sdk 创建了一个符号链接以查找库,我的如下所示,因为 /usr/lib 是输出中的路径之一。如果您想将 Java 库(甚至符号链接)保持在一起,则(在我的输出中)有一个 Java 路径,您可以选择使用而不是 /usr/lib。

sudo ln -s /usr/lib/jvm/java-11-openjdk-amd64/lib/libjawt.so /usr/lib/libjawt.so