无法为Java进程设置LD_LIBRARY_PATH

时间:2022-12-30 22:42:15

I am trying to call my linux executable from shell script. Before calling this executable, I want to set LD_LIBRARY_PATH with specific values. My shell script is as below:

我试图从shell脚本调用我的linux可执行文件。在调用此可执行文件之前,我想设置具有特定值的LD_LIBRARY_PATH。我的shell脚本如下:

Parent.sh (contains 2 lines)

Parent.sh(包含2行)

   - source set_env.sh 
   - executable.so

Set_env.sh

   - setenv LD_LIBRARY_PATH /proj/something

On manually executing Parent.sh scipt from linux console, the executable.so is called with LD_LIBRARY_PATH set correctly. But after integrating it wiht java code as:

在从Linux控制台手动执行Parent.sh scipt时,调用executable.so并正确设置LD_LIBRARY_PATH。但是在将它与java代码集成之后:

String[] commandArray ={"Parent.sh"};
Runtime runtime = Runtime.getRuntime();
Process javap = runtime.exec(commandArray);
javap.waitFor();

LD_LIBRARY_PATH is not set for executable.so

LD_LIBRARY_PATH未设置为executable.so

I hope description is clear :)

我希望描述清楚:)

Please let know whats wrong in the code.

请告诉我们代码中有什么不对。

2 个解决方案

#1


16  

Dunes answer solves your problem, but I would strongly suggest a different approach in this particular case. Instead of relying on a shell to set the environment arguments, you should do this in your Java code. This way you don't need to know which shells exist on the system and what their language is, it will just work on all platforms.

沙丘的答案解决了你的问题,但我强烈建议在这种特殊情况下使用不同的方法。您应该在Java代码中执行此操作,而不是依赖shell来设置环境参数。这样您就不需要知道系统中存在哪些shell以及它们的语言是什么,它只适用于所有平台。

To do this, you can use the Runtime.exec(String[] cmd, String[] environment) overload (javadoc). As the second parameter you can pass an array which contains all the environment variables the subprocess will see.

为此,您可以使用Runtime.exec(String [] cmd,String []环境)重载(javadoc)。作为第二个参数,您可以传递一个包含子进程将看到的所有环境变量的数组。

A little bit nicer even is the ProcessBuilder API:

甚至更好一点的是ProcessBuilder API:

ProcessBuilder pb = new ProcessBuilder("executable.so");
Map<String, String> env = pb.environment();
env.put("LD_LIBRARY_PATH", "/proj/something");
Process javap = pb.start();
javap.waitFor();

This way, the subprocess will inherit all environment variables from the Java process, and additionally have the LD_LIBRARY_PATH variable set.

这样,子进程将从Java进程继承所有环境变量,并另外设置LD_LIBRARY_PATH变量。

#2


1  

Are you sure the subprocess is using csh? If it starts up using bash or something else then this would prevent the script from working (but not to throw an IOException).

你确定子进程正在使用csh吗?如果它使用bash或其他东西启动,那么这将阻止脚本工作(但不会抛出IOException)。

You should really aad a hashbang line as the very first line of your script to state which shell interpreter you wish to use.

你应该真正地使用hashbang行作为脚本的第一行来说明你想要使用哪个shell解释器。

eg.

#!/usr/bin/env csh

#1


16  

Dunes answer solves your problem, but I would strongly suggest a different approach in this particular case. Instead of relying on a shell to set the environment arguments, you should do this in your Java code. This way you don't need to know which shells exist on the system and what their language is, it will just work on all platforms.

沙丘的答案解决了你的问题,但我强烈建议在这种特殊情况下使用不同的方法。您应该在Java代码中执行此操作,而不是依赖shell来设置环境参数。这样您就不需要知道系统中存在哪些shell以及它们的语言是什么,它只适用于所有平台。

To do this, you can use the Runtime.exec(String[] cmd, String[] environment) overload (javadoc). As the second parameter you can pass an array which contains all the environment variables the subprocess will see.

为此,您可以使用Runtime.exec(String [] cmd,String []环境)重载(javadoc)。作为第二个参数,您可以传递一个包含子进程将看到的所有环境变量的数组。

A little bit nicer even is the ProcessBuilder API:

甚至更好一点的是ProcessBuilder API:

ProcessBuilder pb = new ProcessBuilder("executable.so");
Map<String, String> env = pb.environment();
env.put("LD_LIBRARY_PATH", "/proj/something");
Process javap = pb.start();
javap.waitFor();

This way, the subprocess will inherit all environment variables from the Java process, and additionally have the LD_LIBRARY_PATH variable set.

这样,子进程将从Java进程继承所有环境变量,并另外设置LD_LIBRARY_PATH变量。

#2


1  

Are you sure the subprocess is using csh? If it starts up using bash or something else then this would prevent the script from working (but not to throw an IOException).

你确定子进程正在使用csh吗?如果它使用bash或其他东西启动,那么这将阻止脚本工作(但不会抛出IOException)。

You should really aad a hashbang line as the very first line of your script to state which shell interpreter you wish to use.

你应该真正地使用hashbang行作为脚本的第一行来说明你想要使用哪个shell解释器。

eg.

#!/usr/bin/env csh