【问题标题】:Exit jshell with error code退出 jshell 并显示错误代码
【发布时间】:2018-05-11 16:51:59
【问题描述】:

我如何在/exit jshell 会话中使用非零错误代码?

  • /exit 产生:进程以退出代码 0 结束
  • /exit 1 产生:进程以退出代码 0 结束
  • throw new Error("1") 产生:java.lang.Error throw: 1 at (#24:1)` 和 Process finished with exit code 0
  • System.exit(1) 产生:状态引擎已终止。使用以下命令恢复定义:/reload -restore ... 并且 jshell 会话不会终止。

set -e 之类的 bash 命令不可用。

【问题讨论】:

标签: java-9 jshell


【解决方案1】:

现在,JShell 随 JDK 10 一起发布,后来又引入了一个新版本的 /exit,它采用可选的 sn-p 作为参数。该 sn-p 被评估为将返回给调用进程的错误代码。详情请见http://mail.openjdk.java.net/pipermail/kulla-dev/2017-November/002129.html

这是使用 jdk-10+ea-33 的新 /exit 命令的帮助文本:

|  Welcome to JShell -- Version 10
|  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 9 的注意事项:您不能使用 /exit 以非零错误代码退出 JDK 9 上的 jshell 会话。详情请见https://bugs.openjdk.java.net/browse/JDK-8185840

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2017-10-19
    相关资源
    最近更新 更多