
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to debug JShell in Java 9?
JShell is a REPL tool that allows snippets of code to be run without placing them in classes. This tool provides a way to evaluate declarations, statements, and expressions in Java and no need to create the main() method to test some parts of the code.
The command "/debug" can be used to display debugging information for the JShell tool implementation. Once we type the "/debug" command, the debugging mode is on. After enabling the debug mode and type something like a simple addition, or a simple string, then it prints as below.
Example-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
Example-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"
If we want to "off" the debugging mode, then again type "/debug" command for the same session.
jshell> /debug | Debugging off
- Related Articles
- JShell in Java 9?
- How to get JShell documentation in Java 9?
- How to create JShell instance programmatically in Java 9?
- How to reset the JShell session in Java 9?
- How to implement java.time.LocalDate using JShell in Java 9?
- How to import external libraries in JShell in Java 9?
- How to handle an exception in JShell in Java 9?
- How to create scratch variables in JShell in Java 9?
- How to implement a String in JShell in Java 9?
- How to create a thread in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?
- How to initialize an array in JShell in Java 9?
- How to declare reference types in JShell in Java 9?
- How to implement an ArrayList using JShell in Java 9?
- How to save the current JShell session in Java 9?

Advertisements