This tutorial is going provide a quick reference for commands that will frequently encounter when we work with JShell in Java 9. Hopefully, this will be a useful Java 9 JShell cheat sheet.

1. What Is JShell

The JShell tool is a command line tool that provides interactive use of Java Programming Language elements. JShell is a REPL (Read-Evaluate-Print-Loop). With it, we can enter program elements one at a time, immediately seeing the result and adjusting accordingly.

The JShell tool takes two kinds of input:

  • “Snippets” of Java code – declaration and execution.
  • JShell commands – information and control.

2. General JShell Commands

2.1. JShell Help

To display information about JShell commands and subjects, we can use /help as follows:

The [command] can be any JShell command, for example:

The [subject] can be the following values:

  • context: Describes the options that are available for configuring the environment.
  • shortcuts: Describes keystrokes for completing commands and snippets.
  • intro: Provides an introduction to the tool.

For example:

2.2. Set External Editor, Startup Settings, And Feedback Mode

To Sets configuration information, including the external editor, startup settings, and feedback mode, we can use the /set command as follows:

The following values are valid for setting depend on the situations:

2.2.1. Set The Snippet Editor

To sets the command used to start an external editor when the /edit command is entered, the setting is:

The options can be: default, retain, delete, wait.

For example, the following command sets Notepad as an editor on Windows:

2.2.2. Set The Feedback Mode

When you execute a snippet or a command, jshell prints feedback. The amount and format of the feedback depend on the feedback mode, to set the feedback mode, the setting is:

The mode can be silent, concise, normal, verbose.

For example:

2.2.3. Set Startup Snippets

To set the names of the startup scripts used when the next /reset, /reload, or /env command is entered, the setting can be:

  • The scripts can be local files or one of the following predefined scripts: DEFAULT (commonly used imports), JAVASE (Imports all Java SE packages), PRINTING (Defines print, println, and printf as jshell methods for use within the tool).
  • The following options are valid: -default ( default settings), -none (no startup settings are used).

For example, the following command sets the startup-import.jsh file to be the startup script:

And to defines print, println, and printf as JShell methods for use within the tool:

The above commands will enable us to use the print, println, and printf commands in the JShell, for example:

2. JShell Session Commands

2.1. Starting JShell

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

For example:

2.2. Leave/Exit JShell

If we already did enter the JShell tool, we can type /exit to leave or exit:

Let’s see an example:

To get to know more about execute and exit a script with JShell, please visit Java 9 JShell – Execute And Exit A Script.

2.3. Display and Set Environment Settings

To displays the environment settings, or updates the environment settings and restarts the session, we can use the /env command:

If no option is entered, then the current environment settings are displayed.


Firstly, let’s set specify the current directory that is searched to locate the class file:

Next, let’s display all the environment settings:

2.4. Reset or Restart The Session

To discards all entered snippets and restarts the session, we can use the /reset command:

For example:

We can specify the [options] to overwrite the previous settings, for example:

Verify again the above setting:

2.5. Restart The Session

Another command for us to restart the session is the /reload:

The difference between the /reload command with /reset command is the /reload command will not clear any previous snippets before restarting the session, for example:

Firstly, let’s define some snippets:

Next, let’s restart the session with the /reload:

Lastly, let’s verify again all snippets:

And we can see that all the previous snippets are still there, in the session.

4. Snippet Information JShell Commands

Whatever we enter in JShell ends up being part of a snippet and every snippet is assigned a unique snippet ID.

4.1. List Snippets

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

For example,

4.2. List Variables

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

For example,

4.3. List Methods

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

4.4. Display Classes, Interfaces, and Enums

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

For example,

4.5. Display The Current Active Imports

To displays the current active imports, including those from the startup scripts and scripts that were entered on the command line when JShell was started, we can use the /imports command:

For example,

To get to know how to import set class path and use external code, please visit Import The External Library Into Java 9 JShell.

5. Snippet Control JShell Commands

5.1. Editing Snippets

Snippets can be edited in JShell tool. However, It is hard to edit multi-line snippets in the JShell tool. Therefore, to facilitate this, JShell supports us editing the snippets in the editor which can be set by using the /set command. You can refer to the section “Set The Snippet Editor” for more information on how to set up your own editor.

To edit a snippet, we can use the /edit command which has the following forms:

Let’s see an example in the following image:

Java 9 JShell Cheat Sheet - Edit Snippets

Edit Snippets

  • Firstly, in the first block (marked with RED) we create a snippet
  • Secondly, call the /list command to see its id.
  • Thirdly, we set the snippet editor as Visual Studio Code.
  • Lastly, we run the /edit 1 to edit the snippets in the Visual Studio Code.

5.2. Drop Snippets

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

For examples:

5.3. Save Snippets

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

Where [option] can be one of the options: -all, -history, and -start. And file-path is the file path where the snippets and commands will be saved, for example:

5.4. Open Snippets

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

The command can be one of the following forms:

  • DEFAULT: Loads the default entries, which are commonly used imports.
  • JAVASE: Imports all Java SE packages.
  • PRINTING: Defines print, println, and printf as jshell methods for use within the tool.

6. References

Below are other Java 9 related tutorials:

Install Oracle Java 9 on CentOS, RHEL 7

Install Oracle Java 9 on Ubuntu 16.04 LTS (Xenial Xerus)

Set Up Eclipse, IntelliJ And NetBeans For Java 9

Create Immutable Lists In Java 9 By Static Factory Methods

Private Interface Methods In Java 9

Using The InputStream.transferTo() To Copy Streams In Java 9

Java 9 – New Methods Of The Optional Class

Streams API Updates In Java 9

How To Compare Arrays In Java 9

Java 9 HTTP/2 Client API Example

Java 9 – Effectively Final Variables In try-with-resources

0 0 vote
Article Rating