- Java 9:Building Robust Modular Applications
- Dr. Edward Lavieri Peter Verhas Jason Lee
- 650字
- 2025-04-04 17:08:34
Getting Started with JShell
JShell is a command-line tool that is located in the /bin folder. The syntax for this tool is jshell <options> <load files>. As you can see here, there are several options that can be used with this tool:

You have already seen the -h option, that we executed with jshell -h. This provided the listing of JShell options.
To log into your JShell, you simply use the jshell command. You will see that the prompt in the command window changes accordingly:

Exiting the shell is as easy as entering /exit. Once inside the JShell, you can enter any of the following commands:
Command | Functionality |
/drop | Use this command to delete a source entry that is referenced by name or id. Here is the syntax: /drop <name or id> |
/edit | With this command, you can edit a source entry using a name or id reference. Here is the syntax: /edit <name or id> |
/env | This powerful command allows you to view or change the evaluation context. Here is the syntax: /env [-class-path <path>] [-module-path <path>] |
/exit | This command is used to exit the JShell. The syntax is simply /exit without any options or parameters available. |
/history | The history command provides a history of what you have typed. The syntax is simply /history without any options or parameters available. |
/<id> | This command is used to rerun a previous snippet by referencing the id. Here is the syntax: /<id>
You can also run a specific snippet by referencing the n th previous snippet with /-<n>.
|
/imports | You can use this command to list the imported items. The syntax is /imports and it does not accept any options or parameters. |
/list | This command will list the source you typed. Here is the syntax: /list [<name or id> | -all | -start] |
/methods | This command lists all declared methods as well as their signatures. Here is the syntax: /methods [<name or id> | -all | -start] |
/open | Using this command, you can open a file as source input. Here is the syntax: /open <file> |
/reload | The reload command gives you the ability to reset and replay relevant history. Here is the syntax: /reload [-restore] [-quiet] [-class-path |
/reset | This command resets the JShell. Here is the syntax: /reset [-class-path <path>] [-module-path |
/save | This command saves the snippet source to a file specified by you. Here is the syntax: /save [-all | -history | -start] <file> |
/set | This command is used to set the JShell configuration information. Here is the syntax: /set editor | start | feedback | mode | prompt | |
/types | This command simply lists declared types. Here is the syntax: /types [<name or id> | -all | -start] |
/vars | This command lists all declared variables as well as their values. Here is the syntax: /vars [<name or id> | -all | -start] |
/! | This command will rerun the last snippet. The syntax is simply /! |
- ClassDeclaration
- Expression
- FieldDeclaration
- ImportDeclaration
- InterfaceDeclaration
- MethodDeclaration
Entering the /help or /? command in the JShell provides a complete list of commands and syntax that can be used in the shell. That list is provided as follows:

The /help command can be especially helpful if you are still new to JShell. As you can see in the following screenshot, we can obtain an introduction to JShell by simply entering the /help intro command:

If you find yourself using JShell often you might benefit from one or more of the following listed shortcuts. These can be listed at any time from within JShell by using the /help shortcuts command:

Additional help can be obtained from within the JShell by using the /help command followed by the command you want additional help on. For example, entering /help reload provides detailed information regarding the /reload command. That information is provided as follows:
