×
☰ See All Chapters

Java Shell Tool (JShell)

The feature that the java missed for years is a shell tool to execute the code from command line. To evaluate any declarations, statements, and expressions, we need to save it in java file and then compile to class file and then execute it from JVM. This process is no longer necessary with the introduction of Java Shell tool (JShell). The Java Shell tool is an interactive tool can be used to execute and test any declarations, statements, and expressions etc. easily. JShell is a Read-Evaluate-Print Loop (REPL) tool.

Uses of JShell

When you want to test single line statement, as an example System.out.println(20*5);, you have to go through all the below process:

  1. Write a complete program, At least one class and one method should exist in program. 

  2. Compile it to class file and fix any compilation errors. 

  3. Run the program. 

  4. Evaluate the result. 

  5. Edit the program is result is wrong. 

  6. Repeat the above steps. 

But using Jshell tool all the above steps are not necessary, open the tool and test the statements.

jshell-0
 

How to open JShell

  1. Install JDK 9 

  2. Set path in environment variables. To know how to set path in environment variable refer our tutorial  

  3. Open command prompt 

  4. Type jshell and hit Enter 

jshell-1
 

How to close JShell

To close JShell type /exit and hit Enter

jshell-2
 

In JShell Informative messages start with a vertical bar. If line starts from vertical bar, then that is a message.

Storing variables in JShell sessions

When a variable is declared in JShell, they will be stored in the current JShell session and can used at any time in the current session. In the below execution we store a String variable.

jshell-3
 

Stored variable will be shown in JShell using ==> characters. ==> separates the variable name and its value.

No we will access and print the stored variable.

jshell-4
 

When variable entered that doesn't have a name, JShell create assign variable to store the value. These variables start with $ sign. These variables are called as scratch variables.

jshell-5
 

Note:

  • While declaring variables using semicolon at the end of declaration is optional. 

  • The declared variables will have global scope and can be used in any place. 

Storing methods in JShell sessions

Like variables we can store methods in JShell sessions.

jshell-6
 

Continuation prompt (...>) is used when a multiline statements like above is entered.

Calling stored method is same as we call in real programs.

jshell-7
 

Testing Expressions using JShell

JShell can be used to test Java expression to get instant results.

jshell-8
 

Importing Packages to JShell

  • By default below packages are imported in JShell 

    1. import java.io.* 

    2. import java.math.* 

    3. import java.net.* 

    4. import java.nio.file.* 

    5. import java.util.* 

    6. import java.util.concurrent.* 

    7. import java.util.function.* 

    8. import java.util.prefs.* 

    9. import java.util.regex.* 

    10. import java.util.stream.* 

  • Available packages can be seen using /imports command 

jshell-9
 
  • To import any unavailable package use import statement. 

jshell-10
 

JShell Commands

To control the environment and display information within a session, JShell provides the below listed commands

Command

Description

/!

re-run last snippet

/-<n>

re-run n-th previous snippet

/<id>

re-run snippet by id

/?

get information about jshell

/drop

delete a source entry referenced by name or id

/edit

edit a source entry referenced by name or id

/env

view or change the evaluation context

/exit

exit jshell

/help

get information about jshell

/history

history of what you have typed

/imports

list the imported items

/list

list the source you have typed

/methods

list the declared methods and their signatures

/open

open a file as source input

/reload

reset and replay relevant history -- current or previous (-restore)

/reset

reset jshell

/save

Save snippet source to a file.

/set

set jshell configuration information

/types

list the declared types

/vars

list the declared variables and their values

Tab Auto Completion in JShell

This is very useful feature of JShell. You can partially enter the name of the stored declarations, statements and press Tab.

If variable with the name message is stored in session then mess<Tab> will auto populate the variable message

jshell-11
 

If variable with the name message1 and message2 is stored in session then mess<Tab> will auto suggest the variables message1, message2. Autocomplete takes place only when Jshell finds only one stored element. If more than one element found then it auto suggests them.

jshell-12
 

All Chapters
Author