退出带有错误代码的jshell
问题描述:
如何使用非零错误代码/exit
a jshell会话?退出带有错误代码的jshell
-
/exit
收率:过程结束,退出代码0 -
/exit 1
收率:过程结束,退出代码0 -
throw new Error("1")
收率:java.lang.Error的抛出:1在(#24 :1)`和进程用退出码完成0 -
System.exit(1)
收益率:状态引擎终止。使用以下命令恢复定义:/ reload -restore ...并且jshell会话未终止。
像set -e
这样的bash命令不可用。
答
您不能使用/exit
以非零错误代码退出正常的jshell会话。有关详细信息,请参阅https://bugs.openjdk.java.net/browse/JDK-8185840。
但是,如果您以本地执行模式启动jshell(即,它不启动远程虚拟机),您可以使用System.exit(1)
退出本地虚拟机并输入您选择的错误代码。概念运行过程中出现的一个DOS外壳
小证明:
jshell --execution local
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell> System.exit(123)
echo Exit Code is %errorlevel%
Exit Code is 123
详见http://mail.openjdk.java.net/pipermail/kulla-dev/2017-August/002062.html。
更新Java的10
的Java 10将引入的/exit
的新版本有一个可选的片段作为参数。该片段被评估为将返回给调用进程的错误代码。详情请参阅http://mail.openjdk.java.net/pipermail/kulla-dev/2017-November/002129.html。
下面是使用JDK-10 + EA-33新/exit
命令的帮助文本:
| Welcome to JShell -- Version 10-ea
| For an introduction type: /help intro
jshell> /help exit
|
| /exit
|
| Leave the jshell tool. No work is saved.
| Save any work before using this command
|
| /exit
| Leave the jshell tool. The exit status is zero.
|
| /exit <integer-expression-snippet>
| Evaluate the snippet. If the snippet fails or is not an integer expression,
| display the error. Otherwise leave the jshell tool with the
| value of the expression as the exit status
jshell> /exit 123
| Goodbye (123)
现在有一个问题,在JDK的bug跟踪系统:https://bugs.openjdk.java.net/browse/JDK-8185840 – Sormuras
对,我们计划在下一个版本JDK 18.3(又名JDK 10)中修复JDK-8185840中的这个增强功能。 –