Java Shell

Java Shell


3 min read

Java Shell is a command-line tool that provides an interactive way to access the Java .

It lets you evaluate snippets of Java code instead of forcing you to write an entire Java program.

It is a REPL (Read-Eval-Print loop) for Java.

Read-Eval-Print loop (REPL) is a command-line tool (also known as interactive language shell) that lets users evaluate snippets of code quickly without having to write a complete program.

The name REPL means—read, eval, and print—used in a loop. The read function reads the user input and parses into a data structure; the eval function evaluates the parsed user input to yield a result; the print function prints the result.

For beginners, it provides a way to explore the Java programming language quickly.

For experienced developers, it provides a quick way to see results of a code snippet without having to compile and run an entire program.

JShell has its own parser that parses snippets and determines the type of snippets, for example, a method declaration, a variable declaration, etc. Once the snippet type is determined, the snippet is wrapped in a synthetic class using the following rules:

• Import statements are used “as-is”.

• Variables, methods, and class declarations become static members of a synthetic class.

• Expressions and statements are wrapped in a synthetic method of a synthetic class.

All synthetic classes belong to a package called REPL.
Start JShell
help command
/help intro

The DEFAULT argument starts jshell with several import statements
>jshell --start DEFAULT' >jshell --start PRINTING includes all versions of the System class. ex. >println("java")'

>jshell --start DEFAULT --start PRINTING
includes the default import statements and the printing methods:

Exiting JShell:-

> 3 + 3
$1 ==> 6

>/set feedback verbose
>/set feedback -retain verbose
>/set feedback -retain normal
Listing Snippet:-

>/list -all

>list -start

List Variables:-

>/vars -all

>/vars -start

JShell Edit Pad:-
Import Statements:-

>/ import java.time.*

Top-Level-Method Declaration and Forward References:

You can enter a slash (/) and press Tab to see a list of all available jshell commands:

>/history History of all commands
In JShell , you can write a fragment of Java code e and evaluate it. Those fragments of code are known as snippets.

Snippets can be:

• Import declarations

• Class declarations

• Interface declarations

• Method declarations

• Field declarations

• Statements

• Expressions

If a line begins with ...>, it means the snippet is not complete and you need to enter more text to complete the snippet.
You can declare all types such as classes, interfaces, enums, and annotations in jshell as you do in Java.
You can declare and call methods in jshell. You can declare top-level methods, which are methods that are entered in jshell directly and are not inside any class.

In jshell , you do not need to terminate a statement with a semicolon.
following commands set the feedback mode to verbose:-
>/set feedback verbose
verbose feedback mode persists across the jshell sessions:-
>/set feedback -retain verbose
You can also use the /vars command to list all variables defined in jshell:-

code is tested on ubuntu 20.04.6 with java openjdk version ""