我正在尝试为ProcessBuilder对象添加一个环境变量,但是当我在ProcessBuilder中调用该新变量时,我收到一个错误。 这就是我构建过程的方法
public class OTU
{
public static void main(String[] args) throws Exception
{
ProcessBuilder pb = new ProcessBuilder();
Map<String, String> env = pb.environment();
//set environment variable u
env.put("u", "util/");
pb.command("echo $u");
Process p = pb.start();
String output = loadStream(p.getInputStream());
String error = loadStream(p.getErrorStream());
int rc = p.waitFor();
System.out.println("Process ended with rc=" + rc);
System.out.println("\nStandard Output:\n");
System.out.println(output);
System.out.println("\nStandard Error:\n");
System.out.println(error);
}
private static String loadStream(InputStream s) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(s));
StringBuilder sb = new StringBuilder();
String line;
while((line=br.readLine()) != null)
sb.append(line).append("\n");
return sb.toString();
}
}
我收到错误
Exception in thread "main" java.io.IOException: Cannot run program "$u": java.io.IOException: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
at ca.utoronto.siseq_1_2.OTU.main(OTU.java:22)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
at java.lang.ProcessImpl.start(ProcessImpl.java:81)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:468)
... 1 more
如果我只为此Process设置变量,我不明白为什么我会收到错误。 请帮我如何设置env变量,以便我可以在ProcessBuilder中使用它。
答案 0 :(得分:13)
Alfredo O的例子为您提供了正确的想法。您需要告诉ProcessBuilder用于执行命令的程序。在这种情况下bash with the "-c" switch,它告诉bash解释接下来的内容(即“echo $ u”)作为命令。
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
public class OTU {
public static void main(String[] args) throws Exception {
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "echo $u");
Map<String, String> env = pb.environment();
// set environment variable u
env.put("u", "util/");
Process p = pb.start();
String output = loadStream(p.getInputStream());
String error = loadStream(p.getErrorStream());
int rc = p.waitFor();
System.out.println("Process ended with rc=" + rc);
System.out.println("\nStandard Output:\n");
System.out.println(output);
System.out.println("\nStandard Error:\n");
System.out.println(error);
}
private static String loadStream(InputStream s) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(s));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null)
sb.append(line).append("\n");
return sb.toString();
}
}
这会产生以下输出:
Process ended with rc=0
Standard Output:
util/
Standard Error:
答案 1 :(得分:5)
这适用于Windows:
@Test
public void testProcessBuilder() throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "echo Hello %name%");
Map<String, String> environment = processBuilder.environment();
environment.put("name", "Alfredo Osorio");
Process p = processBuilder.start();
String line;
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = r.readLine()) != null) {
System.out.println(line);
}
r.close();
}
<强>输出:强>
Hello Alfredo Osorio
正如您在Windows中看到的,您使用%environmentVariable%而不是$ environementVariable