Java 中的 Process.exitValue()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4842712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:17:25  来源:igfitidea点击:

Process.exitValue() in Java

java

提问by Vicky

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:

下面是我用来从命令行程序中简单地打开和关闭 Internet Explorer 的程序。我在 Windows XP 操作系统上使用 Java 6 运行我的程序:

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.

退出值为:1。

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

Javadoc 说:按照惯例,值 0 表示正常终止。 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'.

然后我评论了 p1.destroy 并没有从我的 Java 程序中关闭浏览器,而是手动关闭了窗口(文件>退出)。在这种情况下,p1.exitValue 开始返回“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..
  1. 为什么程序在第一种情况下将退出代码返回为“1”?JVM 是否将 p1.destroy() 视为终止程序的异常方式?
  2. 通常,“退出状态代码”值是特定于 JVM 的还是特定于操作系统的?我看到一些问题,人们将退出代码值报告为“10”、“34545”等。

Thank you for reading,

感谢您的阅读,

回答by paxdiablo

Actually, that's twoquestions :-)

实际上,这是两个问题:-)

  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.

  1. 几乎可以肯定,IE 本身捕获了它正在外部关闭的事实,并决定返回该错误代码(参见下面的 2)。所以不,JVM 不会将其p1.destroy()视为特例,但受影响的进程可能会。

  2. 退出值是特定于进程的,而不是特定于 JVM 的(甚至不是特定于操作系统的)。换句话说,进程本身返回一个值作为退出值。当您想到有一些方法可以销毁根本不涉及 JVM 的进程时,这是有道理的。



I should mention that there are cases where the process doesn'taffect 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.

我应该提到,在某些情况下,该过程不会影响退出代码。在某些类 UNIX 操作系统下,如果进程由于某些严重故障(例如分段违规或外部暴力关闭)而退出,则操作系统可能会将退出代码设置为指示此情况的值。从记忆中,它类似于 128 加上信号编号。