JVM:如何定义Java可执行文件的退出代码? [英] JVM: how are exit codes of the java executable defined?
问题描述
我正在寻找java
可执行文件的可能退出代码的定义:
I'm looking for a definition of possible exit codes of the java
executable:
(如何确定退出代码是执行的Java进程还是虚拟机本身?)
示例:在Windows上,java -badoption
返回1
; java Main
且Main
是有效的类,也可能会返回1
.
Example: On Windows, java -badoption
returns 1
; java Main
with Main
being a valid class may return 1
as well.
我可以使用任何VM选项使退出代码更有意义吗?例如.区分两种退出代码?
Are there any VM options I could use to make exit codes more meaningful? E.g. to distinguish between both types of exit codes?
如果我知道退出代码不是来自Java进程(仅返回0
),那么非零退出代码是什么意思?
If I know that the exit code is not from my Java process (which only returns 0
), what do non-zero exit codes mean?
在Windows上,我经常看到-1
和1
.由于这些是通过自动错误报告工具报告的,因此我看不到任何错误消息.我只有退出代码,需要对其进行解释.
On Windows, I'm frequently seeing -1
and 1
. As these are reported through an automated bug-reporting facility, I can't see any error messages. I only have the exit code and need to interpret it.
退出代码是否取决于平台?
推荐答案
退出代码的含义(通常)
ISO/IEC 9899(编程语言:C),ISO/IEC 9945(IEEE 1003,POSIX)
没有专门针对Java/JVM定义的内容.这些退出代码的含义由ISO/IEC 9899(编程语言:C,例如7.20.4.3)定义.
Meaning of Exit Codes (in general)
ISO/IEC 9899 (Programming Languages: C), ISO/IEC 9945 (IEEE 1003, POSIX)
There is nothing which is specifically defined for Java / JVM. The meaning of these exit codes is defined by ISO/IEC 9899 (Programming Languages: C, for example 7.20.4.3 The exit
function of ISO/IEC 9899:TC3), ISO/IEC 9945 (IEEE 1003, POSIX) and similar specifications, and it's always like this:
- 0表示成功
- 其他任何值均表示失败
shell环境(sh
,bash
,cmd.exe
,make
等)使用它来确定程序是成功退出(0)还是出错(不是0).
This is used by shell environments (sh
, bash
, cmd.exe
, make
etc. etc.) to determine whether a program exited successfully (0) or there was an error (not 0).
强烈建议仅使用0表示成功.使用0以外的值获得成功的程序会使Shell脚本,Makefile等文件的编写者感到头疼.
It is strongly recommended to use only 0 for success. Programs which use values other than 0 for success cause headache to the authors of shell scripts, Makefiles and alike.
由各个程序来定义不同故障值的其他语义.但是,我认为-1
并不是一个好主意.最好的办法是使用EXIT_SUCCESS
和EXIT_FAILURE
,但是,<stdlib.h>
中的这些定义在Java中没有相应的对应项. ISO/IEC 9899:2011没有描述应如何定义EXIT_FAILURE
和EXIT_SUCCESS
.但是,它将0定义为具有与EXIT_SUCCESS
相同的语义,并且我不知道任何系统除以下功能外还可以执行其他操作,因此这是Java程序员可以假定的最好的事情:
It's up to the individual program to define additional semantics of different failure values. However, I think -1
is not such a good idea. The best thing would be to use EXIT_SUCCESS
and EXIT_FAILURE
, however, these definitions from <stdlib.h>
have no corresponding counterparts in Java. ISO/IEC 9899:2011 does not describe how EXIT_FAILURE
and EXIT_SUCCESS
should be defined. However, it defines 0 as having the same semantics as EXIT_SUCCESS
, and I do not know of any system which does something else than the following, which therefore is the best thing a Java programmer can assume:
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
因此,如果要区分不同类型的故障很重要,我将使用0表示成功,使用1表示失败,并使用不同于1的值.一个很好的例子是grep
命令.从其手册页(GNU grep):
So, I'd use 0 for success, 1 for failure, and values distinct from 1 if it is important to differentiate between different types of failure. A good example is the grep
command. From its man page (GNU grep):
退出状态 如果找到选定的行,则退出状态为0;如果未找到,则退出状态为1.如果发生错误,则退出状态为2.(注意:POSIX错误处理代码应检查'2'或更大.)
EXIT STATUS The exit status is 0 if selected lines are found, and 1 if not found. If an error occurred the exit status is 2. (Note: POSIX error handling code should check for '2' or greater.)
我不使用-1,因为它会设置所有位,并且根据环境的不同,-1可能实际上会意外地生成附加的伪造信息,例如声称信号已发出等.实际上,为了便于移植,除非您真的真的很清楚自己在做什么,否则退出代码应在[0..127]
范围内.
例如,在大多数POSIX系统上,退出代码将被截断为8位,并且高于127的退出代码的语义是退出是由信号引起的.
I'd not use -1 because it sets all bits, and depending on the environment, -1 might actually accidentally generate additonal bogus information, like claiming that signals were raised and alike. Actually, to be portable, the exit code should be within the range [0..127]
, unless you really really really know what you're doing.
For example, on most POSIX systems, the exit code will be truncated to 8 bits, and the semantics of an exit code above 127 is that the exit was caused by a signal.
BSD尝试提供更多退出代码,这些代码可以在/usr/include/sysexits.h
中使用(
BSD has tried to supply a few more exit codes, which are available in /usr/include/sysexits.h
(man page). They are like this:
- 64:命令行使用错误
- 65:数据格式错误
- 66:无法打开输入
- 67:收件人未知
- 68:主机名未知
- 69:服务不可用
- 70:内部软件错误
- 71:系统错误(即无法分叉)
- 72:缺少重要的操作系统文件
- 73:无法创建(用户)输出文件
- 74:输入/输出错误
- 75:临时失败;邀请用户重试
- 76:协议中的远程错误
- 77:权限被拒绝
- 78:配置错误
- 64: command line usage error
- 65: data format error
- 66: cannot open input
- 67: addressee unknown
- 68: host name unknown
- 69: service unavailable
- 70: internal software error
- 71: system error (i.e. can't fork)
- 72: critical OS file missing
- 73: can't create (user) output file
- 74: input/output error
- 75: temp failure; user is invited to retry
- 76: remote error in protocol
- 77: permission denied
- 78: configuration error
在没有其他有意义的标准的情况下,我认为我们可以做的最好的事情就是使用它们.
In the absence of any other meaningful standard, I think the best thing we can do is use them.
在UNIX上,您可以通过屏蔽退出值的低7位(减128)来获得信号,在大多数shell中,可以使用$?
来查询该值.
On UNIX, you can obtain the signal by masking out the lower 7 bits (subtract 128) from the exit value, which in most shells can be queried using $?
.
-
SIGTERM
-> VM:143
SIGTERM
-
SIGSEGV
-> VM:134
SIGABRT
(因为VM处理SIGSEGV来写入hs_err文件,然后调用abort()
).
SIGTERM
-> VM:143
SIGTERM
SIGSEGV
-> VM:134
SIGABRT
(because the VM handles SIGSEGV to write the hs_err file and then callsabort()
).
与普通"程序的比较:
-
SIGSEGV
->编:139
SIGSEGV
SIGSEGV
-> prog:139
SIGSEGV
未为JVM明确指定此行为,它只是POSIX环境中程序的正常预期行为.鉴于JVM希望自己处理SIGSEGV
,并且编写比普通核心文件更具体的崩溃转储,甚至甚至期望SIGABRT
代替SIGSEGV
.
This behavior is not explicitly specified for the JVM, it is just the normal expected behavior of programs in a POSIX environment. Even the SIGABRT
instead of SIGSEGV
is more or less expected, given that the JVM wants to handle SIGSEGV
on its own, writing a more specific crash dump than a normal core file.
待办事项
这篇关于JVM:如何定义Java可执行文件的退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!