【发布时间】:2011-05-04 16:49:12
【问题描述】:
我有一个程序是:
import java.io.*;
import java.util.*;
public class ExecBashCommand {
public static void main(String args[]) throws IOException {
if (args.length <= 0) {
System.err.println("Need command to run");
System.exit(-1);
}
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("./nv0914 < nv0914.challenge");
Process process1 = runtime.exec("echo ${?}");
InputStream is = process1.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
//System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
注意:nv0914 是一个 bash 可执行文件,而 nv0914.challenge 是一个文本文件。
当我在终端中运行任何正常命令时,然后如果我使用echo ${?} 检查退出代码。现在我想通过使用程序来做同样的事情,但程序只是给出输出${?}。请帮帮我!
【问题讨论】:
-
process.exitValue() 有什么问题?
-
我回滚了对问题的最后一个更改,其中 OP 部分实施了 Peter Lawrey 提供的解决方案。这是对问题的重大更改,并且确实使未来的读者无法使用,IMO。
标签: java bash unix exit-code runtime.exec