jshell - interactively evaluate declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL)
jshell [options] [load-files]
One or more scripts to run when the tool is started. Scripts can contain any valid code snippets or JShell commands.
The script can be a local file or one of following predefined scripts:
DEFAULTJAVASEPRINTINGprint, println, and printf as jshell methods for use within the tool.
For more than one script, use a space to separate the names. Scripts are run in the order in which they're entered on the command line. Command-line scripts are run after startup scripts. To run a script after JShell is started, use the /open command.
To accept input from standard input and suppress the interactive I/O, enter a hyphen (-) for load-files. This option enables the use of the jshell tool in pipe chains.
JShell provides a way to interactively evaluate declarations, statements, and expressions of the Java programming language, making it easier to learn the language, explore unfamiliar code and APIs, and prototype complex code. Java statements, variable definitions, method definitions, class definitions, import statements, and expressions are accepted. The bits of code entered are called snippets.
As snippets are entered, they're evaluated, and feedback is provided. Feedback varies from the results and explanations of actions to nothing, depending on the snippet entered and the feedback mode chosen. Errors are described regardless of the feedback mode. Start with the verbose mode to get the most feedback while learning the tool.
Command-line options are available for configuring the initial environment when JShell is started. Within JShell, commands are available for modifying the environment as needed.
Existing snippets can be loaded from a file to initialize a JShell session, or at any time within a session. Snippets can be modified within the session to try out different variations and make corrections. To keep snippets for later use, save them to a file.
--add-modules module[,module...]-Cflag--class-path pathCLASSPATH environment variable. If the environment variable isn't set and this option isn't used, then the current directory is searched. For Oracle Solaris, Linux, and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
--feedback modeSets the initial level of feedback provided in response to what's entered. The initial level can be overridden within a session by using the /set feedback mode command. The default is normal.
The following values are valid for mode:
verbosenormalconcisesilent/set mode command.
--help or -h or -?--help-extra or -X-Jflag--module-path modulepath--no-startup--startup option is used.
-qconcise, which is the same as entering --feedback concise.
-Rflag-ssilent, which is the same as entering --feedback silent.
--show-version--startup fileOverrides the default startup script for this session. The script can contain any valid code snippets or commands.
The script can be a local file or one of the following predefined scripts:
DEFAULTJAVASEPRINTINGprint, println, and printf as jshell methods for use within the tool.
For more than one script, provide a separate instance of this option for each script. Startup scripts are run when JShell is first started and when the session is restarted with the /reset, /reload, or /env command. Startup scripts are run in the order in which they're entered on the command line.
This option can't be used if the --no-startup option is used.
-vverbose, which is the same as entering --feedback verbose.
--versionWithin the jshell tool, commands are used to modify the environment and manage code snippets.
/drop {name|id|startID-endID} [{name|id|startID-endID}...]/list command to see the IDs of code snippets.
/edit [option]Opens an editor. If no option is entered, then the editor opens with the active snippets.
The following options are valid:
-endID} [{name|id|startID-endID}...]/list command to see the IDs of code snippets.
-all-startTo exit edit mode, close the editor window, or respond to the prompt provided if the -wait option was used when the editor was set.
Use the /set editor command to specify the editor to use. If no editor is set, then the following environment variables are checked in order: JSHELLEDITOR, VISUAL, and EDITOR. If no editor is set in JShell and none of the editor environment variables is set, then a simple default editor is used.
/env [options]Displays the environment settings, or updates the environment settings and restarts the session. If no option is entered, then the current environment settings are displayed. If one or more options are entered, then the session is restarted as follows:
Updates the environment settings with the provided options.
Resets the execution state.
Runs the startup scripts.
Silently replays the history in the order entered. The history includes all valid snippets or /drop commands entered at the jshell prompt, in scripts entered on the command line, or scripts entered with the /open command.
Environment settings entered on the command line or provided with a previous /reset, /env, or /reload command are maintained unless an option is entered that overwrites the setting.
The following options are valid:
--add-modules module[,module...]--add-exports source-module/package=target-module[,target-module]*--class-path pathCLASSPATH environment variable. If the environment variable isn't set and this option isn't used, then the current directory is searched. For Oracle Solaris, Linux, and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
--module-path modulepath:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
/exit [integer-expression-snippet]/history/help [command|subject]Displays information about commands and subjects. If no options are entered, then a summary of information for all commands and a list of available subjects are displayed. If a valid command is provided, then expanded information for that command is displayed. If a valid subject is entered, then information about that subject is displayed.
The following values for subject are valid:
contextintroshortcuts/imports/list [option]Displays a list of snippets and their IDs. If no option is entered, then all active snippets are displayed, but startup snippets aren't.
The following options are valid:
-endID} [{name|id|startID-endID}...]-alls are startup snippets. IDs that begin with e are snippets that failed.
-start/methods [option]Displays information about the methods that were entered. If no option is entered, then the name, parameter types, and return type of all active methods are displayed.
The following options are valid:
-endID} [{name|id|startID-endID}...]/list command to see the IDs of code snippets.
-all-start/open fileOpens the script specified and reads the snippets into the tool. The script can be a local file or one of the following predefined scripts:
DEFAULTJAVASEPRINTINGprint, println, and printf as jshell methods for use within the tool.
/reload [options]Restarts the session as follows:
Updates the environment settings with the provided options, if any.
Resets the execution state.
Runs the startup scripts.
Replays the history in the order entered. The history includes all valid snippets or /drop commands entered at the jshell prompt, in scripts entered on the command line, or scripts entered with the /open command.
Environment settings entered on the command line or provided with a previous /reset, /env, or /reload command are maintained unless an option is entered that overwrites the setting.
The following options are valid:
--add-modules module[,module...]--add-exports source-module/package=target-module[,target-module]*--class-path pathCLASSPATH environment variable. If the environment variable isn't set and this option isn't used, then the current directory is searched. For Oracle Solaris, Linux, and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
--module-path modulepath:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
-quiet-restore/reset, /reload, or /env command was executed in the previous run. The valid history since that point is replayed. Use this option to restore a previous JShell session.
/reset [options]Discards all entered snippets and restarts the session as follows:
Updates the environment settings with the provided options, if any.
Resets the execution state.
Runs the startup scripts.
History is not replayed. All code that was entered is lost.
Environment settings entered on the command line or provided with a previous /reset, /env, or /reload command are maintained unless an option is entered that overwrites the setting.
The following options are valid:
--add-modules module[,module...]--add-exports source-module/package=target-module[,target-module]*--class-path pathCLASSPATH environment variable. If the environment variable isn't set and this option isn't used, then the current directory is searched. For Oracle Solaris, Linux, and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
--module-path modulepath:) to separate items in the path. For Windows, use a semicolon (;) to separate items.
/save [options] fileSaves snippets and commands to the file specified. If no options are entered, then active snippets are saved.
The following options are valid:
-endID} [{name|id|startID-endID}...]/list command to see the IDs of the code snippets.
-all-history-start/set [setting]Sets configuration information, including the external editor, startup settings, and feedback mode. This command is also used to create a custom feedback mode with customized prompt, format, and truncation values. If no setting is entered, then the current setting for the editor, startup settings, and feedback mode are displayed.
The following values are valid for setting:
editor [options] [command]Sets the command used to start an external editor when the /edit command is entered. The command can include command arguments separated by spaces. If no command or options are entered, then the current setting is displayed.
The following options are valid:
-default-deleteSets the editor to the one in effect when the session started. If used with the -retain option, then the retained editor setting is deleted and the editor is set to the first of the following environment variables found: JSHELLEDITOR, VISUAL, or EDITOR. If none of the editor environment variables are set, then this option sets the editor to the default editor.
This option can't be used if a command for starting an editor is entered.
-retain-waitfeedback [mode]Sets the feedback mode used to respond to input. If no mode is entered, then the current mode is displayed.
The following modes are valid: concise, normal, silent, verbose, and any custom mode created with the /set mode command.
format mode field "format-string" selectorSets the format of the feedback provided in response to input. If no mode is entered, then the current formats for all fields for all feedback modes are displayed. If only a mode is entered, then the current formats for that mode are displayed. If only a mode and field are entered, then the current formats for that field are displayed.
To define a format, the following arguments are required:
/set mode command can be modified.
/help /set format command.
"format-string"/help /set format command.
/help /set format command.
mode [mode-name] [existing-mode] [options]Creates a custom feedback mode with the mode name provided. If no mode name is entered, then the settings for all modes are displayed, which includes the mode, prompt, format, and truncation settings. If the name of an existing mode is provided, then the settings from the existing mode are copied to the mode being created.
The following options are valid:
-command|-quiet-command to show information and verification feedback for commands. Use -quiet to show only essential feedback for commands, such as error messages.
-delete-retain option with this option. Predefined modes can't be deleted.
-retainConfigure the new feedback mode using the /set prompt, /set format, and /set truncation commands.
To start using the new mode, use the /set feedback command.
prompt mode "prompt-string" "continuation-prompt-string"Sets the prompts for input within JShell. If no mode is entered, then the current prompts for all feedback modes are displayed. If only a mode is entered, then the current prompts for that mode are displayed.
To define a prompt, the following arguments are required:
/set mode command can be modified.
"prompt-string""continuation-prompt-string"start [-retain] [file [file...]|option]Sets the names of the startup scripts used when the next /reset, /reload, or /env command is entered. If more than one script is entered, then the scripts are run in the order entered. If no scripts or options are entered, then the current startup settings are displayed.
The scripts can be local files or one of the following predefined scripts:
DEFAULTJAVASEPRINTINGprint, println, and printf as jshell methods for use within the tool.
The following options are valid:
-default-noneUse the -retain option to save the start setting across sessions.
truncation mode length selectorSets the maximum length of a displayed value. If no mode is entered, then the current truncation values for all feedback modes are displayed. If only a mode is entered, then the current truncation values for that mode are displayed.
To define truncation values, the following arguments are required:
/set mode command can be modified.
/help /set truncation command.
/types [option]Displays classes, interfaces, and enums that were entered. If no option is entered, then all current active classes, interfaces, and enums are displayed.
The following options are valid:
-endID} [{name|id|startID-endID}...]/list command to see the IDs of the code snippets.
-all-start/vars [option]Displays the name, type, and value of variables that were entered. If no option is entered, then all current active variables are displayed.
The following options are valid:
-endID} [{name|id|startID-endID}...]/list command to see the IDs of the code snippets.
-all-start/?/help command.
/!/{name|id|startID-endID} [{name|id|startID-endID}...]/list command to see the IDs of the code snippets.
/-n/-4 runs the 11th snippet. Commands aren't included in the count.
The following shortcuts are available for entering commands and snippets in JShell.
When entering snippets, commands, subcommands, command arguments, or command options, use the Tab key to automatically complete the item. If the item can't be determined from what was entered, then possible options are provided.
When entering a method call, use the Tab key after the method call's opening parenthesis to see the parameters for the method. If the method has more than one signature, then all signatures are displayed. Pressing the Tab key a second time displays the description of the method and the parameters for the first signature. Continue pressing the Tab key for a description of any additional signatures.
An abbreviation of a command is accepted if the abbreviation uniquely identifies a command. For example, /l is recognized as the /list command. However, /s isn't a valid abbreviation because it can't be determined if the /set or /save command is meant. Use /se for the /set command or /sa for the /save command.
Abbreviations are also accepted for subcommands, command arguments, and command options. For example, use /m -a to display all methods.
A history of what was entered is maintained across sessions. Use the up and down arrows to scroll through commands and snippets from the current and past sessions. Use the Ctrl key with the up and down arrows to skip all but the first line of multiline snippets.
Use the Ctrl+R key combination to search the history for the string entered. The prompt changes to show the string and the match. Ctrl+R searches backwards from the current location in the history through earlier entries. Ctrl+S searches forward from the current location in the history though later entries.
The editing capabilities of JShell are similar to that of other common shells. Keyboard keys and key combinations provide line editing shortcuts. The Ctrl key and Meta key are used in key combinations. If your keyboard doesn't have a Meta key, then the Alt key is often mapped to provide Meta key functionality.
| Key or Key Combination | Action |
|---|---|
| Return | Enter the current line. |
| Left arrow | Move the cursor to the left one character. |
| Right arrow | Move the cursor to the right one character. |
| Ctrl+A | Move the cursor to the beginning of the line. |
| Ctrl+E | Move the cursor to the end of the line. |
| Meta+B | Move the cursor to the left one word. |
| Meta+F | Move the cursor to the right one word. |
| Delete | Delete the character under the cursor. |
| Backspace | Delete the character before the cursor. |
| Ctrl+K | Delete the text from the cursor to the end of the line. |
| Meta+D | Delete the text from the cursor to the end of the word. |
| Ctrl+W | Delete the text from the cursor to the previous white space. |
| Ctrl+Y | Paste the most recently deleted text into the line. |
| Meta+Y | After Ctrl+Y, press to cycle through the previously deleted text. |
JShell is provided with the JDK. To start a session, enter jshell on the command line. A welcome message is printed, and a prompt for entering commands and snippets is provided.
% jshell
| Welcome to JShell -- Version 9
| For an introduction type: /help intro
jshell>
To see which snippets were automatically loaded when JShell started, use the /list -start command. The default startup snippets are import statements for common packages. The ID for each snippet begins with the letter s, which indicates it's a startup snippet.
jshell> /list -start
s1 : import java.io.*;
s2 : import java.math.*;
s3 : import java.net.*;
s4 : import java.nio.file.*;
s5 : import java.util.*;
s6 : import java.util.concurrent.*;
s7 : import java.util.function.*;
s8 : import java.util.prefs.*;
s9 : import java.util.regex.*;
s10 : import java.util.stream.*;
jshell>
To end the session, use the /exit command.
jshell> /exit
| Goodbye
%
Snippets are Java statements, variable definitions, method definitions, class definitions, import statements, and expressions. Terminating semicolons are automatically added to the end of a completed snippet if they're missing.
The following example shows two variables and a method being defined, and the method being run. Note that a scratch variable is automatically created to hold the result because no variable was provided.
jshell> int a=4
a ==> 4
jshell> int b=8
b ==> 8
jshell> int square(int i1) {
...> return i1 * i1;
...> }
| created method square(int)
jshell> square(b)
$5 ==> 64
Change the definition of a variable, method, or class by entering it again.
The following examples shows a method being defined and the method run:
jshell> String grade(int testScore) {
...> if (testScore >= 90) {
...> return "Pass";
...> }
...> return "Fail";
...> }
| created method grade(int)
jshell> grade(88)
$3 ==> "Fail"
To change the method grade to allow more students to pass, enter the method definition again and change the pass score to 80. Use the up arrow key to retrieve the previous entries to avoid having to reenter them and make the change in the if statement. The following example shows the new definition and reruns the method to show the new result:
jshell> String grade(int testScore) {
...> if (testScore >= 80) {
...> return "Pass";
...> }
...> return "Fail";
...> }
| modified method grade(int)
jshell> grade(88)
$5 ==> "Pass"
For snippets that are more than a few lines long, or to make more than a few changes, use the /edit command to open the snippet in an editor. After the changes are complete, close the edit window to return control to the JShell session. The following example shows the command and the feedback provided when the edit window is closed. The /list command is used to show that the pass score was changed to 85.
jshell> /edit grade
| modified method grade(int)
jshell> /list grade
6 : String grade(int testScore) {
if (testScore >= 85) {
return "Pass";
}
return "Fail";
}
The feedback mode determines the prompt that's displayed, the feedback messages that are provided as snippets are entered, and the maximum length of a displayed value. Predefined feedback modes are provided. Commands for creating custom feedback modes are also provided.
Use the /set mode command to create a new feedback mode. In the following example, the new mode mymode, is based on the predefined feedback mode, normal, and verifying command feedback is displayed:
jshell> /set mode mymode normal -command
| Created new feedback mode: mymode
Because the new mode is based on the normal mode, the prompts are the same. The following example shows how to see what prompts are used and then changes the prompts to custom strings. The first string represents the standard JShell prompt. The second string represents the prompt for additional lines in multiline snippets.
jshell> /set prompt mymode
| /set prompt mymode "\njshell> " " ...> "
jshell> /set prompt mymode "\nprompt$ " " continue$ "
The maximum length of a displayed value is controlled by the truncation setting. Different types of values can have different lengths. The following example sets an overall truncation value of 72, and a truncation value of 500 for variable value expressions:
jshell> /set truncation mymode 72
jshell> /set truncation mymode 500 varvalue
The feedback displayed after snippets are entered is controlled by the format setting and is based on the type of snippet entered and the action taken for that snippet. In the predefined mode normal, the string created is displayed when a method is created. The following example shows how to change that string to defined:
jshell> /set format mymode action "defined" added-primary
Use the /set feedback command to start using the feedback mode that was just created. The following example shows the custom mode in use:
jshell> /set feedback mymode
| Feedback mode: mymode
prompt$ int square (int num1){
continue$ return num1*num1;
continue$ }
| defined method square(int)
prompt$