可能重复:
How do I get the bash command exit code from a Process run from within Java?java的bash命令的问题
嗨,我已经使用bash命令./nv0914 < nv0914.challenge
,其中nv0914是Unix的可执行文件和程序是一个简单的解密另一个文件nv0914.challenge是用于解密的文本文件。现在,当我从bash运行它时,如果运行命令echo ${?}
,我得到的值是0,这很好。但是现在我必须实施一次攻击,为此我需要通过java程序打印退出值。我有这样的代码,就好像现在:
import java.io.*;
import java.util.*;
public class ExecBashCommand {
public static void main(String args[]) throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("./nv0914 < nv0914.challenge");
/* InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;*/
System.out.println("Exit Code: "+process.exitValue());
//System.out.printf("Output of running %s is:", Arrays.toString(args));
/* while ((line = br.readLine()) != null) {
System.out.println(line);
}*/
}
}
但这个程序是给我的错误:
Exception in thread "main" java.lang.IllegalThreadStateException: process hasn't exited
at java.lang.UNIXProcess.exitValue(UNIXProcess.java:172)
at ExecBashCommand.main(ExecBashCommand.java:16)
任何建议,如何让出口值。谢谢
你应该在你以前的相同问题中看到彼得的答案。它包含答案。你也应该接受它。 – 2011-05-04 17:36:14