如何在 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
How to add .so file to the java.library.path in Linux
提问by Lihkinisak
I am working with a java application which needs a .dll
file in java.library.path in windows. To run same application in Linux I have the respective .so
file which should be added to java.library.path
in linux machine, I didnt find any easy solution for this so far
我正在使用一个 java 应用程序,它需要.dll
在 windows 中的 java.library.path 中的一个文件。要在 Linux 中运行相同的应用程序,我有相应的.so
文件应该添加到java.library.path
linux 机器中,到目前为止我没有找到任何简单的解决方案
I did put the .so
in a folder which is already in the class path, but my application still complains there is no required .so
file in java.library.path
我确实把它.so
放在一个已经在类路径中的文件夹中,但我的应用程序仍然抱怨没有必需的.so
文件java.library.path
I'd like to find:
我想找到:
- Ways to add
.so
tojava.library.path
- How to know if its already added (when added)
- 添加
.so
方法java.library.path
- 如何知道它是否已经添加(添加时)
采纳答案by Jose Luis Martin
Add the containing directory to LD_LIBRARY_PATH
before launching the application
LD_LIBRARY_PATH
在启动应用程序之前将包含目录添加到
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/pathOfContainingDirectory
Use java -XshowSettings:properties
to 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 lib
prefix in the library name.
我在弄清楚这一点时遇到了很多麻烦,请确保您lib
在库名称中有前缀。
So steps,
所以步骤,
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/some/pathOfContainingDirectory"
Rename libraries to have lib as a prefix. [Add this as part of build script]
mv JNIDemo.so libJNIDemo.so
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/some/pathOfContainingDirectory"
重命名库以将 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