如何在 Java 9 中除錯 JShell?


JShell 是一個 REPL 工具,允許執行程式碼片段,而無需將它們放入類中。該工具提供了一種在 Java 中評估宣告語句表示式的方法,並且無需建立 main()方法來測試程式碼的某些部分。

命令 "/debug" 可用於顯示 JShell 工具實現的除錯資訊。一旦輸入 "/debug" 命令,除錯模式即開啟。啟用除錯模式後,鍵入簡單加法或簡單字串之類的內容,然後按如下所示打印出來。

示例 1

jshell> /debug
| Debugging on

jshell> 5+3
Compiling: 5+3
Kind: EXPRESSION_STATEMENT -- 5 + 3;
compileAndLoad [Unit($1)]
++setCompilationInfo() Snippet:VariableKey($1)#11-5+3
package REPL;
import java.io.*;import java.math.*;import java.net.*;import java.nio.file.*;import java.util.*;
import java.util.concurrent.*;import java.util.function.*;import java.util.prefs.*;
import java.util.regex.*;import java.util.stream.*;class $JShell
$11 {
public static
int $1;
public static Object do_it$() throws Throwable {
return $1 = 5+3;
}
}

-- diags: []
setStatus() Snippet:VariableKey($1)#11-5+3 - status: VALID
compileAndLoad ins = [Unit($1)] -- legit = [Unit($1)]
Compiler generating class REPL.$JShell$11
compileAndLoad [Unit($1)] -- deps: [] success: true
recordCompilation: Snippet:VariableKey($1)#11-5+3 -- status VALID, unresolved []

$1 ==> 8


示例 2

jshell> /debug
| Debugging on

jshell> String s = "Adithya"
Compiling: String s = "Adithya";
Kind: VARIABLE -- String s = "Adithya"
compileAndLoad [Unit(s)]
++setCompilationInfo() Snippet:VariableKey(s)#12-String s = "Adithya";
package REPL;
import java.io.*;import java.math.*;import java.net.*;import java.nio.file.*;import java.util.*;
import java.util.concurrent.*;import java.util.function.*;import java.util.prefs.*;
import java.util.regex.*;import java.util.stream.*;import static REPL.$JShell$11.$1;
class $JShell$12 {
public static String s;
public static Object do_it$() throws Throwable {
String s_ =
"Adithya";
return s = s_;
}
}

-- diags: []
setStatus() Snippet:VariableKey(s)#12-String s = "Adithya"; - status: VALID
compileAndLoad ins = [Unit(s)] -- legit = [Unit(s)]
Compiler generating class REPL.$JShell$12
compileAndLoad [Unit(s)] -- deps: [] success: true
recordCompilation: Snippet:VariableKey(s)#12-String s = "Adithya"; -- status VALID, unresolved []
s ==> "Adithya"


如果我們想“關閉”除錯模式,則再次為同一個會話輸入"/debug" 命令。

jshell> /debug
| Debugging off

更新於: 13-3-2020

304 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.