Sormuras
Sormuras

Reputation: 9069

Exit jshell with error code

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

A bash command like set -e is not available.

Upvotes: 7

Views: 1461

Answers (1)

Sormuras
Sormuras

Reputation: 9069

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.

Upvotes: 5

Related Questions