如何在 Java 中设置 unix 动态库路径(LD_LIBRARY_PATH)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13685042/
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 set a unix dynamic library path (LD_LIBRARY_PATH) in Java?
提问by abhishek
I have a binary executable (no source code) which requires this command to be executed in the terminal - export LD_LIBRARY_PATH = "" to link to a library lbtiff.so.3 which I have in a directory. Only after I execute this export command, I can execute the binary, otherwise it gives an error - "error while loading shared libraries: libtiff.so.3...
我有一个二进制可执行文件(无源代码),它需要在终端中执行此命令 - export LD_LIBRARY_PATH = "" 以链接到我在目录中的库 lbtiff.so.3。只有在我执行了这个导出命令之后,我才能执行二进制文件,否则会报错——“加载共享库时出错:libtiff.so.3...
Now, I want to execute this binary from my java code. But simply executing the export command in the runtime does not do anything and the "error while .." error still occurs when I execute the binary from Java. I guess setting the unix specific environment variable LD_LIBRARY_PATH might not be possible from Java - is there a way I can run my binary from Java and it is able to find the libraries? Here's my current code -
现在,我想从我的 java 代码中执行这个二进制文件。但是在运行时简单地执行导出命令并没有做任何事情,并且当我从 Java 执行二进制文件时仍然会出现“error while ..”错误。我猜想从 Java 设置 unix 特定的环境变量 LD_LIBRARY_PATH 可能是不可能的 - 有没有办法可以从 Java 运行我的二进制文件并且它能够找到库?这是我当前的代码 -
Process p = Runtime.getRuntime().exec("bash -c export LD_LIBRARY_PATH=<lib path>");
p = Runtime.getRuntime().exec("<binary path>");
回答by Ian Roberts
Rather than Runtime.exec
, use ProcessBuilder
. That will allow you to specify environment variables when you run the binary that requires them
而不是Runtime.exec
,使用ProcessBuilder
. 这将允许您在运行需要它们的二进制文件时指定环境变量
ProcessBuilder pb = new ProcessBuilder("<binarypath>");
pb.environment().put("LD_LIBRARY_PATH", "<libPath>");
Process p = pb.start();
Your approach with two separate Runtime.exec
calls will not work, because the environment settings you make in the first one only affect that particular Process
, not subsequent processes started by a separate invocation of Runtime.exec
.
您使用两个单独Runtime.exec
调用的方法将不起作用,因为您在第一个调用中进行的环境设置仅影响该特定的Process
,而不影响由单独调用Runtime.exec
.
回答by Philipp Wendler
See my answerto another question. The best way is to not use an external shell to set the environment variable (your code doesn't work because it will not set the variable globally, only for the bash process), but to set the variable from within Java. Much easier and it works (and on all platforms, regardless of which shell is installed).
请参阅我对另一个问题的回答。最好的方法是不使用外部 shell 来设置环境变量(您的代码不起作用,因为它不会全局设置变量,仅用于 bash 进程),而是从 Java 内部设置变量。更容易并且它可以工作(并且在所有平台上,无论安装了哪个 shell)。
回答by Alex
On unix systems you can prepend the variable before executing the command
在 unix 系统上,您可以在执行命令之前添加变量
LD_LIBRARY_PATH=... foo args
Will execute the program foo
with args
using the modified LD_LIBRARY_PATH
将执行该程序foo
与args
使用修改LD_LIBRARY_PATH
Or you could take advantage of the subshell by using:
或者您可以通过使用子shell来利用:
(export LD_LIBRARY_PATH=...; foo args)