Process.exitValue() in Java
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 开发者_StackOverflow中文版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:
- Why program returns exit code as '1' in first case? Does JVM treats p1.destroy() as abnormal way of terminating a program?
- 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,
Actually, that's two questions :-)
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.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.
精彩评论