3

Below is piece of program I was using to simply Open and close the Internet Explorer from my command line program. im running my program with Java 6 on Windows XP OS:

Runtime runtime = Runtime.getRuntime();       
Process p1 = runtime.exec("C:\\Program Files\\Internet Explorer\\iexplore.exe");
Thread.sleep(5000);
p1.destroy();
Thread.sleep(2000);
System.out.println("p1.exitValue(): "+p1.exitValue())

The exit value is : 1.

Javadoc says: by convention, the value 0 indicates normal termination. http://download.oracle.com/javase/6/docs/api/java/lang/Process.html#exitValue()

Then I commented p1.destroy and instead of closing the browser from my Java program, I closed the window manually (File>Exit). In this case p1.exitValue started returning '0'.

My question is:

  1. Why program returns exit code as '1' in first case? Does JVM treats p1.destroy() as abnormal way of terminating a program?
  2. In general the 'exit status code' value is JVM specific or Operating system specific? I have seen some question where people have reported exit code value as '10', '34545' etc..

Thank you for reading,

1
  • I don't know whether this is the issue here or not, but as you correctly noted, the value 0 indicates normal termination. Terminating a process from outside (using Process.destroy which probably uses the Win32 TerminateProcess) is not something I'd consider "normal" termination... Commented Jan 30, 2011 at 11:51

1 Answer 1

8

Actually, that's two questions :-)

  1. Almost certainly, IE itself captured the fact that it was being shut down externally and decided to return that error code (see 2 below). So no, the JVM doesn't treat p1.destroy() as a special case, but the affected process may.

  2. Exit values are process specific, not JVM specific (and not even OS specific). In other words, a process itself returns a value to be used as the exit value. This makes sense when you think of the fact that there are ways to destroy processes which don't involve the JVM at all.


I should mention that there are cases where the process doesn't affect the exit code. Under some UNIX-like operating systems, if a process exits due to some serious fault (like a segmentation violation or a violent external shutdown), the exit code may be set by the OS to a value indicating this. From memory, it was something like 128 plus the signal number.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.