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:
1 |
/help [command|subject] |
The [command] can be any JShell command, for example:
1 |
/help env |
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:
1 2 3 4 5 6 7 8 9 |
shell> /help intro | | 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'. |
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:
1 |
/set [setting] |
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:
1 |
editor [options] [command] |
The options can be: default, retain, delete, wait.
For example, the following command sets Notepad as an editor on Windows:
1 |
jshell> /set editor -retain notepad.exe |
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:
1 |
feedback [mode] |
The mode can be silent, concise, normal, verbose.
For example:
1 |
/set feedback verbose |
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:
1 |
start [-retain] [file[ file...]|option] |
- 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:
1 |
/set start -retain startup-import.jsh |
And to defines print, println, and printf as JShell methods for use within the tool:
1 2 |
jshell> /set start PRINTING jshell> /reload |
The above commands will enable us to use the print, println, and printf commands in the JShell, for example:
1 2 |
jshell> println ("Java 9 JShell Cheat Sheet") Java 9 JShell Cheat Sheet |
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:
1 |
jshell |
For example:
1 2 3 4 5 |
C:\Users\nangs>jshell | Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> |
2.2. Leave/Exit JShell
If we already did enter the JShell tool, we can type /exit to leave or exit:
1 |
/exit |
Let’s see an example:
1 2 |
jshell> /exit | Goodbye |
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:
1 |
/env [<span class="variable">options</span>] |
If no option is entered, then the current environment settings are displayed.
Examples:
Firstly, let’s set specify the current directory that is searched to locate the class file:
1 2 |
jshell> /env -class-path . | Setting new options and restoring state. |
Next, let’s display all the environment settings:
1 2 |
jshell> /env | --class-path . |
2.4. Reset or Restart The Session
To discards all entered snippets and restarts the session, we can use the /reset command:
1 |
/reset [options] |
For example:
1 2 |
jshell> /reset | Resetting state. |
We can specify the [options] to overwrite the previous settings, for example:
1 2 |
jshell> /reset -class-path D:\data\libs | Resetting state. |
Verify again the above setting:
1 2 |
jshell> /env | --class-path D:\data\libs |
2.5. Restart The Session
Another command for us to restart the session is the /reload:
1 |
/reload [options] |
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:
1 2 3 4 5 6 7 8 |
jshell> int sum(int a, int b){ ...> return a+b; ...> } | created method sum(int,int) jshell> sum (2,3); $2 ==> 5 | created scratch variable $2 : int |
Next, let’s restart the session with the /reload:
1 2 3 4 5 6 |
jshell> /reload | Restarting and restoring state. -: int sum(int a, int b){ return a+b; } -: sum (2,3); |
Lastly, let’s verify again all snippets:
1 2 3 4 5 6 |
jshell> /list 1 : int sum(int a, int b){ return a+b; } 2 : sum (2,3); |
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:
1 2 3 4 5 |
/list /list -all /list -start /list <snippet-name> /list <snippet-id> |
For example,
1 2 3 4 5 6 7 8 |
jshell> /list 1 : print ("hello") 2 : println ("Java 9 JShell Cheat Sheet") jshell> /list 2 2 : println ("Java 9 JShell Cheat Sheet") |
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:
1 2 3 4 5 |
/vars /vars <variable-name> /vars <variable-snippet-id> /vars -start /vars -all |
For example,
1 2 3 |
jshell> /vars | String doc = "Java 9 JShell Cheat Sheet" | int age = 1 |
4.3. List Methods
To displays information about the methods that were entered, we can use the /methods command as follows:
1 2 3 4 5 |
/methods /methods <method-name> /methods <method-snippet-id> /methods -start /methods -all |
4.4. Display Classes, Interfaces, and Enums
To displays classes, interfaces, and enums that were entered, we can use the /types command:
1 2 3 4 5 |
/types /types <type-name> /types <type-snippet-id> /types -start /types -all |
For example,
1 2 3 |
jshell> /types | class Student | enum Level |
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:
1 |
/imports |
For example,
1 2 3 4 5 |
jshell> /imports | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* |
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:
1 2 3 4 5 |
/edit /edit <snippet-name> /edit <snippet-id> /edit -start /edit -all |
Let’s see an example in the following image:
- 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:
1 2 |
/drop <snippet-name> /drop <snippet-id> |
For examples:
1 2 3 4 5 |
jshell> int x = 10 x ==> 10 jshell> drop x | replaced variable x, however, it cannot be referenced until class drop is declared |
5.3. Save Snippets
To save snippets and commands in the current session to file, we can use the /save command as follows:
1 |
/save [options] file |
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:
1 |
jshell> /save my-jshell.jsh |
5.4. Open Snippets
To open the script specified and reads the snippets into the tool, we can use the /open command as follows:
1 |
/open file |
The command can be one of the following forms:
1 2 3 4 |
/open my-jshell.jsh /open DEFAULT /open JAVASE /open PRINTING |
- 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
https://docs.oracle.com/javase/9/tools/jshell.htm
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
How To Compare Arrays In Java 9
Java 9 HTTP/2 Client API Example
Java 9 – Effectively Final Variables In try-with-resources