Java JShell Commands

In this tutorial, we will see the usage of commonly used JShell commands with an example.

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.

Complete the Java JShell tutorial at

Java JShell Commands

JShell Start

JShell is included in JDK 9. To launch the JShell tool, we just need to type the jshell command at the command line:

rameshfadatare@Rameshs-MacBook-Air rameshfadatare % jshell

|  Welcome to JShell -- Version 17.0.2

|  For an introduction type: /help intro

JShell Help Intro

The JShell command /help, with a parameter intro, gives you basic guidelines on how you use the tool.

jshell> /help intro





|  The jshell tool allows you to execute Java code, getting immediate results.

|  You can enter a Java definition (variable, method, class, etc), like:  int x = 8

|  or a Java expression, like:  x + x

|  or a Java statement or import.

|  These little chunks of Java code are called 'snippets'.


|  There are also the jshell tool commands that allow you to understand and

|  control what you are doing, like:  /list


|  For a list of commands: /help

Leave/Exit JShell

Use /exit command to exit from JShell:

jshell> /exit

|  Goodbye

Reset or Restart Session

To discard all entered snippets and restart the session, we can use the /reset command:

jshell> /reset

|  Resetting state.

List Snippets

To list all the snippets, we can use the /list command as follows:

jshell> /list

   1 : import java.time.*;

   2 : System.out.println(

   3 : public interface Vehicle {

        String getBrand();


        String speedUp();


        String slowDown();


        default String turnAlarmOn() {

         return "Turning the vehice alarm on.";



        default String turnAlarmOff() {

         return "Turning the vehicle alarm off.";



        static int getHorsePower(int rpm, int torque) {

         return (rpm * torque) / 5252;




List Variables

To display the name, type, and value of variables that were entered, we can use the /vars command as following forms:

jshell> /vars

|    int a = 10

|    String name = "java guides"

|    float f = 10.0

List Methods

Let's first create two methods using JShell:

jshell> void printNumbers(int n) {

   ...> for(int i=0; i< n; i++) {

   ...> System.out.println(i);

   ...> }

   ...> }


|  created method printNumbers(int)

jshell> void printSquaresOfNumbers(int n) {

   ...> for(int i=0; i< n; i++) {

   ...> System.out.println(i*i);

   ...> }

   ...> }


|  created method printSquaresOfNumbers(int)

To displays information about the methods that were entered, we can use the /methods command as follows:

jshell> /methods

|    void printNumbers(int)

|    void printSquaresOfNumbers(int)

Display Classes, Interfaces, and Enums

To displays classes, interfaces, and enums that were entered, we can use the /types command:

jshell> /types

|    interface Vehicle

|    enum Month

View Java Imports

By default, jshell imports some common packages on startup. You can type /imports command to see all the available imports:

jshell> /imports

|    import*

|    import java.math.*

|    import*

|    import java.nio.file.*

|    import java.util.*

|    import java.util.concurrent.*

|    import java.util.function.*

|    import java.util.prefs.*

|    import java.util.regex.*

|    import*

The java.lang package contents are always available in JShell, just as in any Java source code file.

Edit Snippets

To edit a snippet, we can use the /edit command:

jshell> /edit

|  replaced enum Month

Once you enter /edit the command, JShell will open an editor to edit the snippet.

Drop Snippets

To delete a snippet which can be variables, types, imports, etc, we can use the /drop command:

jshell> int abc = 50;

abc ==> 50

jshell> drop abc

|  replaced variable abc, however, it cannot be referenced until class drop is declared

Save Snippets

To save snippets and commands in the current session to file, we can use the /save command as follows:

/save [options] file

jshell> /save dem-jshell.jsh

Open Snippets

To open the script specified and reads the snippets into the tool, we can use the /open command: