Computer >> Máy Tính >  >> Lập trình >> Java

Làm thế nào để gỡ lỗi JShell trong Java 9?


JShell là một công cụ REPL cho phép chạy các đoạn mã mà không cần đặt chúng vào các lớp. Công cụ này cung cấp một cách để đánh giá tuyên bố , tuyên bố biểu thức trong Java và không cần tạo main () phương pháp để kiểm tra một số phần của mã.

Lệnh "/ debug "có thể được sử dụng để hiển thị thông tin gỡ lỗi để triển khai công cụ JShell. Sau khi chúng tôi nhập "/ debug" , chế độ gỡ lỗi đang bật . Sau khi bật chế độ gỡ lỗi và nhập một cái gì đó như một phép cộng đơn giản hoặc một chuỗi đơn giản, sau đó nó sẽ in như bên dưới.

Ví dụ-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


Ví dụ-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"


Nếu chúng ta muốn "tắt" chế độ gỡ lỗi, sau đó nhập lại "/ debug" lệnh cho cùng một phiên.

jshell> /debug
| Debugging off