Java 9 Jshell Tutorial

By | June 27, 2021

In this post of Java 9 Jshell Tutorial, we will talk and learn all the concepts related to JSHELL using various examples.

What is JShell in Java 9?

It is an interactive Java Shell tool that allows us to execute Java code from the shell and shows output immediately.JShell is a REPL (Read Evaluate Print Loop) tool and runs from the command line.

Advantages of JShell

Jshell reduces all the efforts that are required to run a Java program and test business logic. 

Let’s say if you have to print the “Hello World” message then you have to create a first-class then the main method. Inside that main method, you have to use the println function. Almost you have to write 5 to 6 lines of code to print the “Hello world” message.

If we don’t use Jshell, creating of Java program involves many steps.

How to Start JShell ?

To start Jshell, first, we must have installed Java 9 on our machine then open a command prompt and type jshell, and press Enter. It will open a jshell session with a welcome message to the console.

 Print “Hello World” Message:

To display a simple “Hello World” message, write the print function without creating class and method and hit enter.

Note: Putting Semicolon (;) at the end of any statement is optional


We can declare variables and use them anywhere throughout Jshell session. Let’s create some variables.

  1. The semicolon (;) is optional, we can leave it and it works fine. See, variable x created without using a semicolon
    Int x =20
  2. If we don’t provide a variable name, JShell creates an implicit variable to store the value. These variables start with the $ sign. We can use these variables by specifying implicit variables.


We can type and test any valid Java expression to get instant output.

Create Class & Method:

  • To create a class, write source code for the class and call its method by creating an object immediately.
  • To test method business logic, create a method and get results immediately.

Package Imports:

  • By default, 10 packages are imported and can also be imported any package by using the import statement.
  • Let’s see, default imported packages, we can use /imports command.

jshell> import java.sql.*;  

  • Listing import packages and will show available accessible packages.
  • Now a number of packages are 11 including a new one sql.*.

JShell commands

If you go to the installation location and look into /jdk-9.x.x/bin folder. You will find the jshell.exe file in here.

  1. /help [<command>|<subject>]

get information about jshell

  1. /exit

exit from jshell terminal

  1. /set feedback mode

set jshell configuration information

  1. /list [<name or id>|-all|-start]

list the source you have typed

  1. /reset [-class-path <path>] [-module-path <path>] [-add-modules <modules>]…

reset jshell

  1. /edit <name or id>

Use this command to edit any source entry referenced by name or id

  1. /drop <name or id>

You acn delete a any source entry referenced by name or id

  1. /save [-all|-history|-start] <file>

Save snippet source to a file.

  1. /open <file>

open a file as source input

  1. /vars [<name or id>|-all|-start]

list the declared variables and their values

  1. /methods [<name or id>|-all|-start]

list the declared methods and their signatures

  1. /types [<name or id>|-all|-start]

list the declared types

  1. /imports

list the imported items

view or change the evaluation context

  1. /history

history of what you have typed

  1. /!

re-run the last snippet

  1. /<id>

re-run snippet by id

That’s all about  JShell in Java 9

You May Also Like:

Java 9 private Methods in Interfaces
Java 9 Try with Resource Enhancement
Java 9 @SafeVarargs Annotation
Java 9 Collection Factory Methods
Java 9 Stream API improvements

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *