【发布时间】:2016-12-21 12:58:28
【问题描述】:
我在使用 processbuider 添加到进程环境的路径时遇到问题。我不知道为什么该过程忽略了环境。这是我的例子:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
public class main {
public static void main(String [ ] args) {
try {
String s = null;
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "fsl");
Map<String, String> env;
env = pb.environment();
env.put("FSLDIR", "/usr/local/fsl/bin/");
Process p = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
System.out.println("Process p:");
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
//////////*********\\\\\\\\\\\
ProcessBuilder pb2 = new ProcessBuilder("/usr/local/fsl/bin/fsl");
s = null;
Process p2 = pb2.start();
stdInput = new BufferedReader(new
InputStreamReader(p2.getInputStream()));
stdError = new BufferedReader(new
InputStreamReader(p2.getErrorStream()));
System.out.println("Process p2:");
// read the output from the command
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
Process p:
/bin/bash: fsl: command not found
Process p2:
DISPLAY is not set. Please set your DISPLAY environment variable!
您看到 FSL 想要设置更多变量。这就是为什么 p2 不是一个选项的原因。
【问题讨论】:
-
您在
the process is ignoring the environment中谈论的环境是什么? -
它的 pb.environment() System.out.println(env);实际上表明该目录已添加
-
进程
p报错是合乎逻辑的,因为bash找不到命令和fsl。FSLDIR=/usr/local/fsl/bin/之类的东西对 bash 的行为没有影响 -- 必须更改PATH变量才能使bash找到fsl命令。 -
感谢 Jdamian,它成功了。
标签: java bash processbuilder