2017-06-18 70 views
6

如何使用非零錯誤代碼/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命令不可用。

+0

現在有一個問題,在JDK的bug跟蹤系統:https://bugs.openjdk.java.net/browse/JDK-8185840 – Sormuras

+1

對,我們計劃在下一個版本JDK 18.3(又名JDK 10)中修復JDK-8185840中的這個增強功能。 –

回答

4

您不能使用/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)