JShell is a tool that lets you execute Java code interactively. It is part of the JDK, starting with Java SE 9.
JShell commands control the environment and display information within a session.
Useful commands:
| Command | Description |
|---|---|
/exit |
Exits JShell (nothing is saved). |
/help |
Displays help about using JShell. |
/history |
Lists the snippets and commands entered. |
/imports |
Lists the current active imports. |
/list |
Lists the snippets entered. |
/methods |
Lists the methods entered. |
/reset |
Resets JShell (clears snippets entered). |
/save <file> |
Saves the snippets entered. |
/save <id> <file> |
Saves the snippet specified. |
/vars |
Lists the variables entered. |
/! |
Reevaluates the last snippet. |
/<id> |
Reevaluates the snippet specified. |
Setting an external editor:
/set editor geditEditing all existing snippets:
/editWhen you save in the editor, any changed or new snippet is entered into the JShell session.
Editing a specific snippet:
/edit <id>The feedback mode determines the prompts, feedback, and other interactions within JShell.
Showing the current feedback mode:
/set feedbackSetting the feedback mode:
/set feedback verbose
/set feedback normal
/set feedback concise
/set feedback silentA JShell script is a sequence of snippets and JShell commands in a file, one snippet or command per line.
Starting JShell with executing a script:
$ jshell <file>Reading and executing a script in JShell:
/open <file>Code in the default package can't be accessed from JShell.
-
Apache Maven: jshell-maven-plugin
-
Gradle: jshell-plugin
-
IntelliJ IDEA: JShell Console