Mav*_*ick 5 java unix bash runtime.exec exit-code
我有一个程序是:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:nv0914是一个bash可执行文件,nv0914.challenge是一个文本文件.当我在终端中运行任何正常命令时,如果我使用后检查退出代码echo ${?}.现在我想通过使用程序来做同样的事情,但程序只是提供输出${?}.请帮帮我!
Pet*_*rey 17
Process.waitFor()返回一个返回int退出代码的地方.如果你启动一个不相关的程序,将没有先前的退出代码,这就是为什么它不返回任何有用的东西.
您无法可靠地使用exitValue(),因为您可以在程序完成之前调用它.你应该只在调用waitFor()之后调用它(它会给出相同的退出代码waitFor()吗)
从Javadoc for exitValue
抛出:IllegalThreadStateException - 如果此Process对象表示的子进程尚未终止.
| 归档时间: |
|
| 查看次数: |
7064 次 |
| 最近记录: |