java bash命令问题
嗨,我有一个程序,它是一个简单的使用 bash 命令解密另一个文件的程序 ./nv0914
./nv0914
./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)
有关如何获取退出值的任何建议。谢谢
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.challenge
where 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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试读取退出值之前,您是否尝试过调用 process.waitFor() ?
Have you tried calling
process.waitFor()
before you try and read the exit value?