java-9 - 使用错误代码退出 jshell

标签 java-9 jshell

我该怎么做 /exit jshell具有非零错误代码的 session ?

  • /exit产量:进程完成,退出代码 0
  • /exit 1产量:进程完成,退出代码 0
  • throw new Error("1")产量:java.lang.Error throws: 1 at (#24:1)` 和进程完成退出代码 0
  • System.exit(1)产量:状态引擎终止。使用以下命令恢复定义:/reload -restore ... 并且 jshell session 不会终止。

  • set -e 这样的 bash 命令不可用。

    最佳答案

    现在,JShell 附带 JDK 10 后来又推出了新版本的/exit它将一个可选的片段作为参数。该片段被评估为将返回到调用进程的错误代码。见 http://mail.openjdk.java.net/pipermail/kulla-dev/2017-November/002129.html详情。

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

    |  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 session 。见 https://bugs.openjdk.java.net/browse/JDK-8185840详情。

    关于java-9 - 使用错误代码退出 jshell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44620232/

    相关文章:

    java - 有没有办法删除 JShell 中的历史记录?

    java - C++ 中的模块路径到 Java JNI 调用

    java - JDK9 NetBeans Dev - JNI 教程期间编译错误

    java - 在 Java9 中将孵化器 Http 类与 Maven 一起使用

    java - 我已将所有 jar 文件存储到特定文件夹中。如何将特定文件夹配置到 Jshell 类路径?

    java-9 - 如何将参数传递给jshell脚本?

    java - 我可以从 Java 模块中排除导出的包吗?

    eclipse-plugin - 如何将 org.eclipse.swt(和其他插件依赖项)添加为自动 Java9 模块?

    java - Java 9 jshell 可以用于在另一个 JVM 中运行代码吗?

    java - 从 Jshell 运行 echo 示例