1

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

Community
  • 1
  • 1
Maverick
  • 2,738
  • 24
  • 91
  • 157

1 Answers1

7

Have you tried calling process.waitFor() before you try and read the exit value?

Petey B
  • 11,439
  • 25
  • 81
  • 101