1

我想使用带有 PS4 环境变量集的 Java 在调试模式下运行我的 bash 脚本。

ProcessBuilder pb = new ProcessBuilder("/bin/bash" ,"-x", "/junk/leaptest.sh");
Map<String, String> env = pb.environment();
env.put("PS4", "${BASH_SOURCE}:::${LINENO}:::COV:");
final Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
PrintWriter pw = new PrintWriter(process.getOutputStream());
String line;
while ((line = br.readLine()) != null) {
    System.out.println("line no--->>" + line);
    pw.println("2000");
    pw.flush();
}

我得到的输出因为它没有在调试模式下运行并且我的 PS4 env 没有设置

当我使用运行时命令时,同样的事情,

String[] cmd = new String[3];
cmd[0] = "/bin/bash"; // should exist on all POSIX systems
cmd[1] = "-xv";
cmd[2] = args[0];

String[] env = new String[1];
env [0] = "PS4=${BASH_SOURCE}:::${LINENO}::: COV:";
Runtime rt = Runtime.getRuntime();
System.out.println("Execing " + cmd[0]  + cmd[1]);

Process proc = rt.exec(cmd,env);
4

1 回答 1

1

当使用bash运行时-x,它将跟踪信息写入标准错误,而不是标准输出。您需要通过以下方式捕获它:

PrintWriter pwe = new PrintWriter(process.getErrorStream());
于 2012-07-17T20:17:24.467 回答