Retrieving process exit code in case of ThreadInterrupted

I just created a process via an exec () call, and now I use its .waitFor () method. I need to catch an InterruptedException, but I'm not sure what I should put in the catch code block. I would like to get an exit code, but I won’t if the current thread is interrupted. What should I do to get the exit code of the process if the thread is interrupted?

Example:

import java.io.IOException;


public class Exectest {
public static void main(String args[]){
      int exitval;

      try {
        Process p = Runtime.getRuntime().exec("ls -la ~/");
        exitval = p.waitFor();
        System.out.println(exitval);
    } catch (IOException e) {
        //Call failed, notify user.
    } catch (InterruptedException e) {
        //waitFor() didn't complete. I still want to get the exit val. 
        e.printStackTrace();
    }

}
}
+2
source share
1 answer

If I were you, I would put this in a catch block:

p.destroy();
exitval = p.exitValue();

, - . destroy() , exitValue() ( ).

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

+6

All Articles