What are the useful commands in JShell in Java 9?



In this article, we will learn about useful commands in JShell. Java 9 has introduced a new interactive tool called JShell. This tool can be used to execute, test user-friendly and easy way of Java classes, interfaces, enums, objects, statements and etc. 

Different Useful Commands in JShell

Below are some of the important commands in JShell:

/open

To execute a script after JShell has started, we will use the "/open" command. Scripts are executed in the order they are entered on the command line.

Example

Below is an example to load the "Test.java" file into JShell using the "/open" command:

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> /open c:\temp\Test.java

/var

The "/var" command can be used to get a list of all variables used. While performing the calculations, JShell creates implicit variables. As soon as we type the /var command, it displays all variables declared so far.

The following are different "/var" commands in JShell:

  • /vars [ID]
  • /vars [Variable_Name]
  • /vars -start
  • /vars -all

Example

Below is an example to show the assigned values of $1, $2, and $3 using the "/var" command:

jshell> 2+5
$1 ==> 7

jshell> 8%3
$2 ==> 2

jshell> 9/3
$3 ==> 3

jshell> /var
| int $1 = 7
| int $2 = 2
| int $3 = 3

/types [option]

The "/types "command displays the type of all classes, interfaces, and enums. The [option] can be a specific name or id in which we want to see the type.

The following are different "/types" commands in JShell:

  • /types [ID]
  • /types [Type_Name]
  • /types -start
  • /types -all

Example

Below is an example that shows the types of "Test1" and "Test2" using the "/types "command:

jshell> class Test1 {
...>       void testMethod1() {
...>          System.out.println("TutorialsPoint");
...>    }
...> }
| created class Test1

jshell> /types Test1
| class Test1

jshell> /types Test2
| No such snippet: Test2

/methods [option]

The "/methods" command provides us all methods declared so far. If no option is entered, then the name, parameter types, and return type of all active methods are displayed.

The following are different "/methods" commands in JShell:

  • /methods [ID]
  • /methods [Variable_Name]
  • /methods -start
  • /methods -all

Example

Below is an example of creating a method demo() and showing it using the "/methods" command:

jshell> String demo(String firstName, String lastName) {
...>       return firstName + lastName;
...>    }
| created method demo(String, String)

jshell> /methods
| String demo(String, String)

/list

The "/list" command is one of the most useful commands in JShell. It provides us all the snippets created so far.

The following are different "/list" commands in JShell:

  • /list [ID]
  • /list [Code_Name]
  • /list -start
  • /list -all

Example

Below is an example showing 5 snippets when we execute the "/list" command in JShell:

jshell> /list

1 : 2+5
2 : 8%3
3 : 9/3
4 : class Test1 {
       void testMethod1() {
          System.out.println("TutorialsPoint");
       }
    } 
5 : String demo(String firstName, String lastName) {
       return firstName + lastName;
    }

/help

The "/help" command shows all available JShell commands with brief descriptions. If no options are entered, then a summary of information for all commands and a list of available subjects are displayed.

Example

Below is an example to show the execution of the "/help" command in JShell:

C:\Users\User>jshell
| Welcome to JShell -- Version 9.0.4
| For an introduction type: /help intro

jshell> /help
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-06-10T18:42:06+05:30

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements