我想使用设置了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环境没有设置
当我使用Runtime命令时,
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);发布于 2012-07-18 04:17:24
当bash使用-x运行时,它将跟踪信息写入标准错误,而不是标准输出。你需要用下面这样的东西来捕捉它:
PrintWriter pwe = new PrintWriter(process.getErrorStream());https://stackoverflow.com/questions/10847904
复制相似问题