Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit jshell with error code

Tags:

java-9

jshell

How do I /exit a jshell session with a non-zero error code?

  • /exit yields: Process finished with exit code 0
  • /exit 1 yields: Process finished with exit code 0
  • throw new Error("1") yields: java.lang.Error thrown: 1 at (#24:1)` and Process finished with exit code 0
  • System.exit(1) yields: State engine terminated. Restore definitions with: /reload -restore ... and the jshell session is not terminated.

A bash command like set -e is not available.

like image 388
Sormuras Avatar asked Jun 18 '17 22:06

Sormuras


1 Answers

Now, JShell shipping with JDK 10 and later introduced a new version of /exit that takes an optional snippet as an argument. That snippet is evaluated to the error code that will be returned to the calling process. See http://mail.openjdk.java.net/pipermail/kulla-dev/2017-November/002129.html for details.

Here is the help text for the new /exit commands using 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)

Note for JDK 9: You can't use /exit to exit a jshell session on JDK 9 with a non-zero error code. See https://bugs.openjdk.java.net/browse/JDK-8185840 for details.

like image 166
Sormuras Avatar answered Nov 07 '22 17:11

Sormuras