Week 142 — What is jshell?

Question of the Week #142
What is jshell?
12 Replies
Eric McIntyre
Eric McIntyre2mo ago
The JDK (Java Development Kit) provides a tool named jshell that can be used to run Java code in a REPL environment. This means that one can run the jshell command and enter one or multiple lines of Java code which will get executed without requiring to put that code in a class or method. For example, when running jshell and typing System.out.println("Hello World");, it will print Hello World:
$ jshell
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> System.out.println("Hello World");
Hello World

jshell>
$ jshell
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> System.out.println("Hello World");
Hello World

jshell>
When running an expression in jshell, it will be automatically assigned to a variable consisting of $ and an increasing number:
jshell> 13+37
$2 ==> 50

jshell> System.out.println($2);
50
jshell> 13+37
$2 ==> 50

jshell> System.out.println($2);
50
Classes and methods can be executed in jshell by typing the code for these classes and methods and then using them as one would use them in any other code. It is also possible to redefine methods by typing them again with a different body:
jshell> void test() {
...> System.out.println("test 1");
...> }
| created method test()

jshell> test();
test 1

jshell> void test() {
...> System.out.println("test 2");
...> }
| modified method test()

jshell> test();
test 2
jshell> void test() {
...> System.out.println("test 1");
...> }
| created method test()

jshell> test();
test 1

jshell> void test() {
...> System.out.println("test 2");
...> }
| modified method test()

jshell> test();
test 2
jshell comes with the ability to automatically import classes (or other types). To use that, navigate to the class and press Shift+Tab followed by the i key. This shows a prompt asking what to import. By typing the number, it will automatically import the type.
jshell> LocalDate.now()
| Error:
| cannot find symbol
| symbol: variable LocalDate
| LocalDate.now()
| ^-------^

jshell> LocalDate.now()
0: Do nothing
1: import: java.time.LocalDate
Choice:
Imported: java.time.LocalDate
jshell> LocalDate.now()
$2 ==> 2025-08-30
jshell> LocalDate.now()
| Error:
| cannot find symbol
| symbol: variable LocalDate
| LocalDate.now()
| ^-------^

jshell> LocalDate.now()
0: Do nothing
1: import: java.time.LocalDate
Choice:
Imported: java.time.LocalDate
jshell> LocalDate.now()
$2 ==> 2025-08-30
In addition to that, jshell has many capabilities similar to other shells like bash. For example, one can use the Up- and Down arrow keys to navigate between executed statements to execute them again or use the Left and Right arrow keys to edit something within a line before executing it. It also provides many shortcuts that can also be found in shells like bash. For example, Ctrl+R can be used to start a backwards i-search allowing to type something and jshell will find the last entered line/statement containing that text.
jshell> import java.time.*

jshell> LocalDate.now()
$2 ==> 2025-08-30

jshell> LocalDateTime.now()
$3 ==> 2025-08-30T11:02:13.911616957

jshell> LocalDateTime.now()
bck-i-search: Local
jshell> import java.time.*

jshell> LocalDate.now()
$2 ==> 2025-08-30

jshell> LocalDateTime.now()
$3 ==> 2025-08-30T11:02:13.911616957

jshell> LocalDateTime.now()
bck-i-search: Local
When pressing Ctrl+R again, it will continue searching until it finds another executed statement/line containing that text:
jshell> import java.time.*

jshell> LocalDate.now()
$2 ==> 2025-08-30

jshell> LocalDateTime.now()
$3 ==> 2025-08-30T11:02:13.911616957

jshell> LocalDate.now()
bck-i-search: Local
jshell> import java.time.*

jshell> LocalDate.now()
$2 ==> 2025-08-30

jshell> LocalDateTime.now()
$3 ==> 2025-08-30T11:02:13.911616957

jshell> LocalDate.now()
bck-i-search: Local
Pressing Enter will execute it like any other text. Similar bash shortcuts like pressing Tab for automatic completion, Ctrl+A to go to the start of the line and Ctrl+E to go the end of a line are supported as well. It is also possible to use some commands starting with / within jshell. For example, typing /help will display a list of commands and /imports shows the current imports.
$ jshell
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> /imports
| 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.*
$ jshell
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> /imports
| 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.*
jshell comes with multiple startup scripts that simplify its use. When only running jshell, it imports the classes in the packages listed above. Other scripts can be added by running jshell followed by the name of the script. For example, jshell PRINTING runs jshell with the PRINTING script that provides utility methods to print text to the console without needing to type System.out.println:
$ jshell PRINTING
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> println("Hello World")
Hello World
$ jshell PRINTING
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> println("Hello World")
Hello World
The JAVASE script automatically imports all public classes in exported classes of the Java SE modules. This can be useful to just use classes without needing to import them explicitly.
$ jshell JAVASE
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> LocalDateTime.now()
$182 ==> 2025-08-30T11:24:02.82315626
$ jshell JAVASE
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> LocalDateTime.now()
$182 ==> 2025-08-30T11:24:02.82315626
The TOOLING script provides methods one can use to execute some common tools provided by the JDK directly from jshell.
Eric McIntyre
Eric McIntyre2mo ago
$ jshell TOOLING
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> javac("--version")
javac 24.0.1

jshell> class A{}
| created class A

jshell> javap(A.class)
Classfile /tmp/TOOLING-13675444490414974677.class
Last modified 30 Aug 2025; size 276 bytes
SHA-256 checksum 36214ab8eac741ff02519ebee87aae7a13caa4ad8f7522d9188db62ab42cf431
Compiled from "$JShell$23.java"
public class REPL.$JShell$23$A
minor version: 0
major version: 68
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #7 // REPL/$JShell$23$A
super_class: #2 // java/lang/Object
interfaces: 0, fields: 0, methods: 1, attributes: 3
Constant pool:
#1 = Methodref #2.#3 // java/lang/Object."<init>":()V
#2 = Class #4 // java/lang/Object
#3 = NameAndType #5:#6 // "<init>":()V
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Class #8 // REPL/$JShell$23$A
#8 = Utf8 REPL/$JShell$23$A
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 SourceFile
#12 = Utf8 $JShell$23.java
#13 = Utf8 NestHost
#14 = Class #15 // REPL/$JShell$23
#15 = Utf8 REPL/$JShell$23
#16 = Utf8 InnerClasses
#17 = Utf8 A
{
public REPL.$JShell$23$A();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 5: 0
}
SourceFile: "$JShell$23.java"
NestHost: class REPL/$JShell$23
InnerClasses:
public static #17= #7 of #14; // A=class REPL/$JShell$23$A of class REPL/$JShell$23
$ jshell TOOLING
| Welcome to JShell -- Version 24.0.1
| For an introduction type: /help intro

jshell> javac("--version")
javac 24.0.1

jshell> class A{}
| created class A

jshell> javap(A.class)
Classfile /tmp/TOOLING-13675444490414974677.class
Last modified 30 Aug 2025; size 276 bytes
SHA-256 checksum 36214ab8eac741ff02519ebee87aae7a13caa4ad8f7522d9188db62ab42cf431
Compiled from "$JShell$23.java"
public class REPL.$JShell$23$A
minor version: 0
major version: 68
flags: (0x0021) ACC_PUBLIC, ACC_SUPER
this_class: #7 // REPL/$JShell$23$A
super_class: #2 // java/lang/Object
interfaces: 0, fields: 0, methods: 1, attributes: 3
Constant pool:
#1 = Methodref #2.#3 // java/lang/Object."<init>":()V
#2 = Class #4 // java/lang/Object
#3 = NameAndType #5:#6 // "<init>":()V
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Class #8 // REPL/$JShell$23$A
#8 = Utf8 REPL/$JShell$23$A
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 SourceFile
#12 = Utf8 $JShell$23.java
#13 = Utf8 NestHost
#14 = Class #15 // REPL/$JShell$23
#15 = Utf8 REPL/$JShell$23
#16 = Utf8 InnerClasses
#17 = Utf8 A
{
public REPL.$JShell$23$A();
descriptor: ()V
flags: (0x0001) ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 5: 0
}
SourceFile: "$JShell$23.java"
NestHost: class REPL/$JShell$23
InnerClasses:
public static #17= #7 of #14; // A=class REPL/$JShell$23$A of class REPL/$JShell$23
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre2mo ago
JShell, or the Java Shell, is an interactive command-line tool introduced in Java 9. It functions as a Read-Evaluate-Print Loop (REPL) environment for the Java programming language. Key features and uses of JShell: Interactive Code Execution: JShell allows developers to enter Java code snippets (declarations, statements, expressions) directly into the command line and receive immediate feedback on their execution. Rapid Prototyping and Experimentation: It is ideal for quickly testing out small pieces of code, exploring new API features, or trying different variations of methods without the overhead of creating a full Java class or project. Learning and Exploration: JShell provides a hands-on way for beginners to learn Java fundamentals and for experienced developers to experiment with unfamiliar APIs or language features. Reduced Boilerplate: Unlike traditional Java development, JShell eliminates the need for class definitions, main methods, and compilation steps for simple code snippets, making it more efficient for quick tests. Dynamic Evaluation: It evaluates code snippets as they are entered, displaying results directly in the console, facilitating a dynamic and iterative development process. In essence, JShell provides a lightweight and interactive environment for working with Java code, making it a valuable tool for both learning and rapid development.
Submission from kirannarayan
Eric McIntyre
Eric McIntyre2mo ago
It is a Interactive command line tool, It is used as a Read Eval Print Loop
Submission from kasinathan2008
Eric McIntyre
Eric McIntyre2mo ago
A interactive shell in the terminal, where you can code in java. Just like with the python shell.
Submission from marcl4000
Eric McIntyre
Eric McIntyre2mo ago
It's a Java REPL (Read, Evaluate, Print, Loop). It is utilized for interacting with java code directly, as if it were commands on a command line.
Submission from jessesousa
Eric McIntyre
Eric McIntyre2mo ago
jshell is a java shell, is a interactive command line tool in java, It is very help for beginners to learn and experiment new concepts in java and can practice small code snippets with out main method and class definitions, which is more efficient and quick tests and displays results directly inthe console
Submission from soft.nithin
Eric McIntyre
Eric McIntyre2mo ago
jshell is a program that responds to your statements without writing full java program.
Submission from gaucheyy
Eric McIntyre
Eric McIntyre2mo ago
JShell allows you to write and execute Java code interactively, line by line in you command line. You can also run entire scripts to test or prototype functionality quickly. It works similarly to the python command, providing a REPL (Read-Eval-Print Loop) environment for Java.
Submission from jazdazkurwami911
Eric McIntyre
Eric McIntyre2mo ago
jshell is a new "Read-Evaluate-Print-Loop" (REPL) tool introduced with Java 9. It performs a similar function to REPLs in other languages, such as the interactive modes of python or node-- allow a user to explore the language and experiment with libraries. It provides a much quicker feedback loop than "traditional" Java, which would require saving code in one or more source files, compiling it, and then running it. Here is an example jshell session:
❯ jshell
| Welcome to JShell -- Version 24.0.2
| For an introduction type: /help intro

jshell> import java.time.LocalDateTime

jshell> import java.time.format.DateTimeFormatter

jshell> LocalDateTime.now().format(DateTimeFormatter.IS
ISO_DATE ISO_DATE_TIME ISO_INSTANT ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME ISO_LOCAL_TIME
ISO_OFFSET_DATE ISO_OFFSET_DATE_TIME ISO_OFFSET_TIME ISO_ORDINAL_DATE ISO_TIME ISO_WEEK_DATE
ISO_ZONED_DATE_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOC
ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME ISO_LOCAL_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DA
ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
$3 ==> "2025-08-26T07:53:55.038607"

jshell> /exit
| Goodbye
❯ jshell
| Welcome to JShell -- Version 24.0.2
| For an introduction type: /help intro

jshell> import java.time.LocalDateTime

jshell> import java.time.format.DateTimeFormatter

jshell> LocalDateTime.now().format(DateTimeFormatter.IS
ISO_DATE ISO_DATE_TIME ISO_INSTANT ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME ISO_LOCAL_TIME
ISO_OFFSET_DATE ISO_OFFSET_DATE_TIME ISO_OFFSET_TIME ISO_ORDINAL_DATE ISO_TIME ISO_WEEK_DATE
ISO_ZONED_DATE_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOC
ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME ISO_LOCAL_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DA
ISO_LOCAL_DATE ISO_LOCAL_DATE_TIME
jshell> LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
$3 ==> "2025-08-26T07:53:55.038607"

jshell> /exit
| Goodbye
You can see from the above that semicolons are optional. JShell adds them to snippets automatically, when possible. It also supports expression completion using the Tab key, which was used to complete the DateTimeFormatter.ISO_LOCAL_DATE_TIME constant. You can also see that simply entering an expression caused the string representation to be printed (the line starting with $3). This expression was not assigned to a variable, nor explicitly written to stdout with a println. But JShell writes the result of expressions to stdout automatically, which provides quicker feedback to the user. That said, nearly everything that can be done in a Java program can be done in JShell, such as importing types, defining new classes and interfaces, and assigning to variables:
jshell> var now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
now ==> "2025-08-26T08:03:45.754952"

jshell> public class MyClass {
...> private String name;
...> public MyClass(String name) {
...> this.name = name;
...> }
...> @Override
...> public String toString() {
...> return name;
...> }
...> }
| created class MyClass

jshell> var myClass = new MyClass("Eric")
myClass ==> Eric

jshell> myClass
myClass ==> Eric

jshell> now
now ==> "2025-08-26T08:03:45.754952"

jshell> System.out.printf("%s%n", now)
2025-08-26T08:03:45.754952
$8 ==> java.io.PrintStream@30946e09
jshell> var now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
now ==> "2025-08-26T08:03:45.754952"

jshell> public class MyClass {
...> private String name;
...> public MyClass(String name) {
...> this.name = name;
...> }
...> @Override
...> public String toString() {
...> return name;
...> }
...> }
| created class MyClass

jshell> var myClass = new MyClass("Eric")
myClass ==> Eric

jshell> myClass
myClass ==> Eric

jshell> now
now ==> "2025-08-26T08:03:45.754952"

jshell> System.out.printf("%s%n", now)
2025-08-26T08:03:45.754952
$8 ==> java.io.PrintStream@30946e09
Just as with other expressions, simply entering a variable name causes its string representation to be printed
Eric McIntyre
Eric McIntyre2mo ago
JShell has a number of "slash commands", which control the shell itself. Within jshell, type /<tab> to get a list of the commands. Hit Tab a second time to see a summary of each command. The most import are probably: - /help or /? - Shows JShell help - /exit - Quits the JShell session
⭐ Submission from dangerously_casual
Eric McIntyre
Eric McIntyre2mo ago
$ jshell
| Welcome to JShell -- Version 21
| Type /help for help

jshell> int a = 5;
a ==> 5

jshell> int b = 7;
b ==> 7

jshell> a + b
$3 ==> 12
$ jshell
| Welcome to JShell -- Version 21
| Type /help for help

jshell> int a = 5;
a ==> 5

jshell> int b = 7;
b ==> 7

jshell> a + b
$3 ==> 12
Submission from hmemmy

Did you find this page helpful?