java bash 命令问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5887471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java bash command problem
提问by Maverick
Possible Duplicate:
How do I get the bash command exit code from a Process run from within Java?
Hi, I have a program which is a simple decrypts another file using bash command ./nv0914 < nv0914.challengewhere nv0914 is a unix executable file and nv0914.challenge is a text file used for decryption. Now when i am running it from bash and just after that if I run the command echo ${?}I am getting a value 0, which is good. But now I have to implement an attack and for that I need to get the exit value printed through a java program. I have this code as if now:
嗨,我有一个程序,它是使用 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);
}*/
}
}
But this program is giving me an error :
但是这个程序给了我一个错误:
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)
Any suggestions as to how to get the exit value. Thanks
关于如何获得退出值的任何建议。谢谢
回答by Petey B
Have you tried calling process.waitFor()before you try and read the exit value?
process.waitFor()在尝试读取退出值之前,您是否尝试过调用?

