不能为Java进程设置LD_LIBRARY_PATH [英] Not able to set LD_LIBRARY_PATH for Java process

查看:261
本文介绍了不能为Java进程设置LD_LIBRARY_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



Parent.sh(包含2行)

   -  source set_env.sh 
- executable.so

Set_env.sh

   -  setenv LD_LIBRARY_PATH / proj / something 

从linux控制台手动执行Parent.sh scipt时,会使用LD_LIBRARY_PATH正确设置来执行executable.so。但是在将java代码集成到一起之后:

  String [] commandArray = {Parent.sh}; 
运行时runtime = Runtime.getRuntime();
进程javap = runtime.exec(commandArray);
javap.waitFor();

LD_LIBRARY_PATH未设置为executable.so



我希望描述清楚:)



请知道代码中有什么问题。

解决方案

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



为了做到这一点,你可以使用 Runtime.exec(String [] cmd,String [] environment) overload( javadoc )。作为第二个参数,您可以传递一个数组,其中包含子进程将看到的所有环境变量。



即使是 ProcessBuilder API:

  ProcessBuilder pb = new ProcessBuilder(executable.so); 
映射< String,String> env = pb.environment();
env.put(LD_LIBRARY_PATH,/ proj / something);
进程javap = pb.start();
javap.waitFor();

这样,子进程将从Java进程继承所有的环境变量,另外还有code> LD_LIBRARY_PATH 变量集。


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:

Parent.sh (contains 2 lines)

   - 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:

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

LD_LIBRARY_PATH is not set for executable.so

I hope description is clear :)

Please let know whats wrong in the code.

解决方案

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.

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.

A little bit nicer even is the 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆