JShell is an interactive Java Shell tool used to execute Java statements, expressions, methods, and other code directly from the command line. It follows the Read-Evaluate-Print Loop (REPL) model and is useful for learning Java, testing code, and exploring APIs.
- JShell is a Java REPL introduced in JDK 9.
- It supports snippets such as expressions, statements, variables, methods, classes, and imports.
Why Use JShell in Java?
Normally, testing a small piece of Java code involves writing a source file, compiling it, running it, and modifying it when errors occur. JShell shortens this process by evaluating individual pieces of code immediately.
A typical Java development cycle is:
- Write the program in an editor.
- Save the source file.
- Compile the program.
- Fix compilation errors.
- Run the program.
- Fix runtime errors.
- Repeat the process.
JShell evaluates Java statements, methods, expressions, and classes interactively, making it useful for learning, testing ideas, and exploring APIs. It complements an IDE rather than replacing it.
How to Start and Stop JShell
JShell is included with the JDK starting from JDK 9. Make sure JDK 9 or later is installed before starting JShell.
Steps to Start JShell
Step 1: Open Command Prompt or a terminal.
Step 2: Enter the following command:
jshell
Step 3: Press Enter. The jshell> prompt appears, indicating that JShell is ready to accept Java code.

Steps to Stop JShell
Step 1: Enter the /exit command at the JShell prompt.
jshell> /exit
Step 2: JShell terminates the session and displays:
Goodbye
Print Hello Java Using JShell
JShell does not require a class declaration for a simple statement. Enter the following statement directly:
jshell> System.out.println("Hello Java");
Hello Java

Explanation: JShell evaluates the System.out.println() statement immediately and displays the result without requiring a .java source file or an explicit main() method.
Snippets in JShell
A snippet is a piece of Java code entered into JShell. JShell supports expressions, statements, variable declarations, method declarations, class definitions, and import declarations as snippets.
Variable Snippet
jshell> int x = 11
x ==> 11
| created variable x : int
The variable x is created with the value 11.
Expression Snippet
An expression that does not have a variable name receives a generated scratch variable:
jshell> 8 + 8
$3 ==> 16
| created scratch variable $3 : int
The result is stored in $3 and is available for later use.
Method Snippet
Methods also work directly inside JShell:
jshell> String doubleText(String s) {
...> return s + s;
...> }
| created method doubleText(String)
jshell> doubleText("Geeks")
$5 ==> "GeeksGeeks"
The ...> prompt indicates that JShell is waiting for the remaining lines of a multi-line snippet.
Exceptions in JShell
JShell displays exceptions directly in the interactive session when an entered statement or expression fails during execution. It shows the exception type and relevant details, which helps identify and debug errors without stopping the JShell session.
Example
jshell> int a = 10 / 0|Exception java.lang.ArithmeticException: / by zero|at (#1:1)
Here, ArithmeticException occurs because an integer is divided by zero. JShell displays the exception and remains available for executing the next statement.
Another Example
jshell> String str = null;str ==> nulljshell>System.out.println(str.length());| Exception java.lang.NullPointerException:|at (#3:1)
Here, NullPointerException occurs because length() is called on a null reference.
Handling Exceptions in JShell
Exceptions also follow normal Java exception-handling rules. We use try-catch to handle an exception without displaying it as an unhandled exception.
jshell> try {
...> int result = 10 / 0;...> } catch (ArithmeticException e) {...> System.out.println("Cannot divide by zero");...> }Cannot divide by zero
Here, the ArithmeticException is caught by the catch block, and the specified message is displayed. The JShell session continues normally.
Startup Script in JShell
JShell loads a default startup script when a session begins. It contains commonly used imports, which makes frequently used Java classes available without manually importing them each time. The startup entries are not included in the normal /list output. Use /list -start or /list -all to view them.
jshell> /list -start
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
Saving and Loading Snippets
JShell provides commands for preserving code entered during a session.
To save active snippets:
jshell> /save MyCode.jsh
To load snippets from a file:
jshell> /open MyCode.jsh
A script may also be supplied when starting JShell:
jshell MyCode.jsh
This is useful for keeping JShell experiments and reusing them in later sessions.
Advantages of JShell
- Provides immediate feedback while writing Java code.
- Removes the need to create a complete class for small experiments.
- Supports expressions, statements, variables, methods, classes, and imports.
- Helps test unfamiliar Java APIs interactively.
- Provides commands to inspect, save, load, and manage snippets.
- Supports experimentation before adding tested code to a regular Java application.
JShell Commands
JShell commands control the session and provide information about the entered snippets. Unlike Java snippets, JShell commands begin with a forward slash /.
| Command | Description |
|---|---|
/list | Displays entered snippets |
/vars | Displays active variables |
/methods | Displays defined methods |
/types | Displays defined types |
/imports | Displays active imports |
/history | Displays entered input history |
/save | Saves snippets to a file |
/open | Loads snippets from a file |
/reset | Resets the JShell session |
/help | Displays help information |
/exit | Exits JShell |