java bash命令问题

时间:2011-05-04 17:33:41

标签: java bash exit-code runtime.exec

  

可能重复:
  How do I get the bash command exit code from a Process run from within Java?

嗨,我有一个简单的程序,使用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)

有关如何获取退出值的任何建议。谢谢

1 个答案:

答案 0 :(得分:7)

在尝试读取退出值之前,您是否尝试过调用process.waitFor()