JSHELL(1)
JSHELL(1) β JDK Commands π«Άπ
πΈ NAME πΈ
jshell β interactively evaluate declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL). Bestie, it's literally a whole REPL for Java. β¨
π SYNOPSIS π
jshell [options] [load-files]
options
Command-line options, separated by spaces. See Options for jshell. π
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 these predefined scripts:
DEFAULTβ Loads the default entries, which are commonly used as imports.JAVASEβ Imports all Java SE packages.PRINTINGβ Definesprint,println, andprintfasjshellmethods for use within the tool.TOOLINGβ Definesjavac,jar, and other methods for running JDK tools via their command-line interface within thejshelltool.
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. π
π DESCRIPTION π
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. πΎπ
π§ OPTIONS FOR JSHELL π§
--add-exports module/packageβ Specifies a package to be considered as exported from its defining module.--add-modules module[,module...]β Specifies the root modules to resolve in addition to the initial module.-Cflagβ Provides a flag to pass to the compiler. To pass more than one flag, provide an instance of this option for each flag or flag argument needed.--class-path pathβ Specifies the directories and archives that are searched to locate class files. This option overrides the path in theCLASSPATHenvironment variable. If the environment variable isn't set and this option isn't used, then the current directory is searched. For Linux and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.--enable-previewβ Allows code to depend on the preview features of this release.--execution specificationβ Specifies an alternate execution engine, where specification is anExecutionControlspec. See the documentation of the packagejdk.jshell.spifor the syntax of the spec.--feedback modeβ Sets 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 modecommand. The default isnormal.
The following values are valid for mode:
verbose
Provides detailed feedback for entries. Additional information about the action performed is displayed after the result of the action. The next prompt is separated from the feedback by a blank line.
normal
Provides an average amount of feedback. The next prompt is separated from the feedback by a blank line.
concise
Provides minimal feedback. The next prompt immediately follows the code snippet or feedback.
silent
Provides no feedback. The next prompt immediately follows the code snippet.
custom
Provides custom feedback based on how the mode is defined. Custom feedback modes are created within JShell by using the /set mode command.
--helpor-hor-?β Prints a summary of standard options and exits the tool.--help-extraor-Xβ Prints a summary of nonstandard options and exits the tool. Nonstandard options are subject to change without notice. π-Jflagβ Provides a flag to pass to the runtime system. To pass more than one flag, provide an instance of this option for each flag or flag argument needed.--module-path modulepathβ Specifies where to find application modules. For Linux and macOS, use a colon (:) to separate items in the path. For Windows, use a semicolon (;) to separate items.--no-startupβ Prevents startup scripts from running when JShell starts. Use this option to run only the scripts entered on the command line when JShell is started, or to start JShell without any preloaded information if no scripts are entered. This option can't be used if the--startupoption is used.-qβ Sets the feedback mode toconcise, which is the same as entering--feedback concise.-Rflagβ Provides a flag to pass to the remote runtime system. To pass more than one flag, provide an instance of this option for each flag or flag argument to pass.-sβ Sets the feedback mode tosilent, which is the same as entering--feedback silent.--show-versionβ Prints version information and enters the tool.--startup fileβ Overrides 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:
DEFAULTβ Loads the default entries, which are commonly used as imports.JAVASEβ Imports all Java SE packages.PRINTINGβ Definesprint,println, andprintfasjshellmethods for use within the tool.TOOLINGβ Definesjavac,jar, and other methods for running JDK tools via their command-line interface within thejshelltool.
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.
-vβ Sets the feedback mode toverbose, which is the same as entering--feedback verbose.--versionβ Prints version information and exits the tool.
π JSHELL COMMANDS π
Within the jshell tool, commands are used to modify the environment and manage code snippets. π«Ά
/drop {name|id|startID-endID} [{name|id|startID-endID}...]β Drops snippets identified by name, ID, or ID range, making them inactive. For a range of IDs, provide the starting ID and ending ID separated with a hyphen. To provide a list, separate the items in the list with a space. Use the/listcommand 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:
{name|id|startID-endID} [{name|id|startID-endID}...]β Opens the editor with the snippets identified by name, ID, or ID range. For a range of IDs, provide the starting ID and ending ID separated with a hyphen. To provide a list, separate the items in the list with a space. Use the/listcommand to see the IDs of code snippets.-allβ Opens the editor with all snippets, including startup snippets and snippets that failed, were overwritten, or were dropped.-startβ Opens the editor with startup snippets that were evaluated when JShell was started.
To 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
/dropcommands entered at thejshellprompt, in scripts entered on the command line, or scripts entered with the/opencommand.
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...]β which root modules do we resolve on top of the initial module? like, the extra credit modules bestie π list them separated by commas and jshell will go get 'em.--add-exports source-module/package=target-module[,target-module]*β adds an export ofpackagefromsource-moduletotarget-moduleπ«Ά yes you literally write it assource-module/package=target-module, and yeah the target side can be a whole comma-separated list, slay.--class-path pathβ tells jshell which directories and archives to search for class files π. ngl this one overrides whatever's in theCLASSPATHenvironment variable, so it wins. and if the env var isn't set and you don't use this option? then the current directory gets searched, periodt. path separators: on Linux and macOS use a colon (:), on Windows use a semicolon (;) π--module-path modulepathβ where to find application modules β¨ same separator rules bestie: colon (:) for Linux and macOS, semicolon (;) for Windows.
/exit [integer-expression-snippet] π
exits the tool, bye bestie π. if you don't enter a snippet, the exit status is zero. if you do enter a snippet and the result of that snippet is an integer, then that result becomes the exit status β iconic behavior β¨. but if an error occurs, or the result of the snippet is not an integer? an error is displayed and the tool remains active π like, you're not going anywhere.
/history π
displays what was entered in this session. that's it, that's the whole command. your receipts, preserved π
/help [command|subject] π
displays information about commands and subjects π¦. no options entered? then you get a summary of info for all the commands plus a list of the available subjects. give it a valid command and you get expanded info for that command. give it a valid subject and you get the tea on that subject β¨
the following values for subject are valid:
contextβ describes the options available for configuring the environment πintroβ provides an introduction to the tool πshortcutsβ describes the keystrokes for completing commands and snippets. see Input Shortcuts π
/imports π¦
displays the current active imports β including the ones from the startup scripts and the scripts that were entered on the command line when JShell was started. so like, the full import lore, no cap π«Ά
/list [option] π
displays a list of snippets and their IDs β¨. no option entered? then you get all the active snippets β but startup snippets aren't included, fyi π
the following options are valid:
{name|id|startID-endID} [{name|id|startID-endID}...]β displays the snippets identified by name, ID, or ID range. for a range of IDs, provide the starting ID and ending ID separated with a hyphen. to provide a list, separate the items in the list with a space π-allβ displays all snippets, including startup snippets and snippets that failed, were overwritten, or were dropped. IDs that begin withsare startup snippets. IDs that begin witheare snippets that failed π iykyk-startβ displays startup snippets that were evaluated when JShell was started β
/methods [option] π§
displays information about the methods that were entered. no option entered? then you get the name, parameter types, and return type of all the active methods π«Ά
the following options are valid:
{name|id|startID-endID} [{name|id|startID-endID}...]β displays information for methods identified by name, ID, or ID range. for a range of IDs, provide the starting ID and ending ID separated with a hyphen. to provide a list, separate the items in the list with a space. use the/listcommand to see the IDs of code snippets π-allβ displays information for all methods, including those added when JShell was started, and methods that failed, were overwritten, or were dropped π₯-startβ displays information for startup methods that were added when JShell was started β¨
/open file π
opens the script specified and reads the snippets into the tool π¦. the script can be a local file, or one of these predefined scripts:
DEFAULTβ loads the default entries, which are commonly used as imports πJAVASEβ imports all Java SE packages πPRINTINGβ definesprint,println, andprintfas jshell methods for use within the tool π¨οΈTOOLINGβ definesjavac,jar, and other methods for running JDK tools via their command-line interface within the jshell tool π¨
/reload [options] π
restarts the session, and here's the whole routine bestie:
- 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
/dropcommands entered at the jshell prompt, in scripts entered on the command line, or scripts entered with the/opencommand π
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...]β specifies the root modules to resolve in addition to the initial module πΈ--add-exports source-module/package=target-module[,target-module]*β adds an export ofpackagefromsource-moduletotarget-moduleπ«Ά--class-path pathβ specifies the directories and archives that are searched to locate class files. this option overrides the path in theCLASSPATHenvironment variable. if the environment variable isn't set and this option isn't used, then the current directory is searched. for Linux and macOS, use a colon (:) to separate items in the path. for Windows, use a semicolon (;) to separate items π--module-path modulepathβ specifies where to find application modules. for Linux and macOS, use a colon (:) to separate items in the path. for Windows, use a semicolon (;) to separate items β¨-quietβ replays the valid history without displaying it. errors are displayed π-restoreβ resets the environment to the state at the start of the previous run of the tool, or to the last time a/reset,/reload, or/envcommand was executed in the previous run. the valid history since that point is replayed. use this option to restore a previous JShell session βͺ main character energy fr
/reset [options] π§Ό
discards all entered snippets and restarts the session like so:
- 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 π like gone gone, bestie. no undo.
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...]β specifies the root modules to resolve in addition to the initial module πΈ--add-exports source-module/package=target-module[,target-module]*β adds an export ofpackagefromsource-moduletotarget-moduleπ«Ά--class-path pathβ specifies the directories and archives that are searched to locate class files. this option overrides the path in theCLASSPATHenvironment variable. if the environment variable isn't set and this option isn't used, then the current directory is searched. for Linux and macOS, use a colon (:) to separate items in the path. for Windows, use a semicolon (;) to separate items π--module-path modulepathβ specifies where to find application modules. for Linux and macOS, use a colon (:) to separate items in the path. for Windows, use a semicolon (;) to separate items β¨
/save [options] file πΎ
saves snippets and commands to the file specified π. no options entered? then the active snippets get saved, periodt.
the following options are valid:
{name|id|startID-endID} [{name|id|startID-endID}...]β saves the snippets and commands identified by name, ID, or ID range. for a range of IDs, provide the starting ID and ending ID separated with a hyphen. to provide a list, separate the items in the list with a space. use the/listcommand to see the IDs of the code snippets π¦-allβ saves all snippets, including startup snippets and snippets that were overwritten or failed π-historyβ saves the sequential history of all commands and snippets entered in the current session π-startβ saves the current startup settings. if no startup scripts were provided, then an empty file is saved π« (yes, literally an empty file, that's not a bug it's just how it is)
/set [setting] βοΈ
sets configuration information β including the external editor, startup settings, and feedback mode π. this command is also the one you use to create a custom feedback mode with customized prompt, format, and truncation values, so yeah it does a lot π. 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/editcommand is entered. the command can include command arguments separated by spaces. if no command or options are entered, then the current setting is displayed β¨
π -default
Sets the editor to the default editor that ships with JShell. That's it, that's the whole vibe β back to factory settings, bestie.
Heads up tho: you can't use this one if you entered a command for starting an editor. It's giving "pick one or the other" π
π -delete
Rewinds the editor to whatever was in effect when the session started. Clean slate energy β¨
But wait, there's lore: if you use it together with -retain, then the retained editor setting gets yeeted and the editor is set to the first of these environment variables that it finds, in this order:
JSHELLEDITORVISUALEDITOR
And if none of the editor environment variables are set? Then this option just sets the editor to the default editor. Filling in the blanks, iconic of it honestly π«Ά
Same rule as above applies: this option can't be used if a command for starting an editor is entered.
π -retain
Saves your editor setting across sessions so it sticks around like a good group-chat memory π If you don't enter any other option or a command, then the current setting is what gets saved. Simple, lowkey efficient.
π -wait
Prompts you to indicate when editing is complete. Without it, control returns to JShell the moment the editor exits. Use this option if the editor you're using exits immediately β like, for example, when an edit window already exists π
Fr fr: this option is valid only when a command for starting an editor is entered. Don't come for me, that's literally what the docs say.
π¬ feedback [mode]
Sets the feedback mode used to respond to your input. No mode entered? Then it just displays the current mode, like a lil mirror πΈ
The valid modes are: concise, normal, silent, verbose, plus any custom mode you cooked up yourself with the /set mode command. Custom modes are main character energy, we love to see it π
π§ format mode field "format-string" selector
Sets the format of the feedback you get back in response to input. And the "no args" behaviour is a whole ladder of display options:
- If no mode is entered, the current formats for all fields for all feedback modes are displayed.
- If only a mode is entered, the current formats for that mode are displayed.
- If only a mode and field are entered, the current formats for that field are displayed.
To actually define a format, these arguments are required:
modeβ specifies the feedback mode the response format is applied to. Only custom modes created with the/set modecommand can be modified, so no touching the predefined ones bestie πfieldβ specifies a context-specific field the response format is applied to. The fields are described in the online help, which you get to from JShell using/help /set format."format-string"β the string to use as the response format for the specified field and selector. The structure of the format string is described in the online help too, again via/help /set format.selectorβ specifies the context in which the response format is applied. The selectors are described in the online help, accessed from JShell using/help /set format.
(Yes, all three trails point to the same help command. It's the source of truth, iykyk π )
π mode [mode-name] [existing-mode] [options]
Creates a custom feedback mode with the mode name you provide. Obsessed, honestly.
- No mode name entered? Then the settings for all modes are displayed, which includes the mode, prompt, format, and truncation settings.
- Name of an existing mode provided? Then the settings from that existing mode get copied over to the mode being created. Hand-me-downs but make it tech β¨
The following options are valid:
-command|-quietβ specifies the level of feedback displayed for commands when using the mode. This option is required when creating a feedback mode.-commandshows information and verification feedback for commands.-quietshows only essential feedback for commands, such as error messages. Choose your fighter π«Ά-deleteβ deletes the named feedback mode for this session. The name of the mode to delete is required. Want it gone permanently? Use-retaintogether with this option. Predefined modes can't be deleted β they're protected, periodt.-retainβ saves the named feedback mode across sessions. The name of the mode to retain is required.
Configure your shiny new feedback mode using the /set prompt, /set format, and /set truncation commands. Then to actually start using it, hit up the /set feedback command. Go off π
π¦ prompt mode "prompt-string" "continuation-prompt-string"
Sets the prompts for input within JShell. The display logic:
- No mode entered β current prompts for all feedback modes are displayed.
- Only a mode entered β current prompts for that mode are displayed.
To define a prompt, these arguments are required:
modeβ the feedback mode the prompts are applied to. Only custom modes created with the/set modecommand can be modified."prompt-string"β the string to use as the prompt for the first line of input."continuation-prompt-string"β the string to use as the prompt for the additional input lines needed to complete a snippet. That's the "wait, I'm not done typing" prompt, and it deserves respect π€
π 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, they run in the order you entered them. No scripts or options entered? Then the current startup settings are displayed, cute and informative πΈ
The scripts can be local files, or one of these predefined scripts:
DEFAULTβ loads the default entries, which are commonly used as imports.JAVASEβ imports all Java SE packages.PRINTINGβ definesprint,println, andprintfas jshell methods for use within the tool.TOOLINGβ definesjavac,jar, and other methods for running JDK tools via their command-line interface within the jshell tool.
The following options are valid:
-defaultβ sets the startup settings to the default settings.-noneβ specifies that no startup settings are used. Absolutely nothing. Empty. Peaceful π§
Use the -retain option to save the start setting across sessions.
β truncation mode length selector
Sets the maximum length of a displayed value β the anti-rambling setting, we could all use one π
- No mode entered β current truncation values for all feedback modes are displayed.
- Only a mode entered β current truncation values for that mode are displayed.
To define truncation values, these arguments are required:
modeβ the feedback mode the truncation value is applied to. Only custom modes created with the/set modecommand can be modified.lengthβ the unsigned integer to use as the maximum length for the specified selector. Unsigned, bestie, don't try anything funny.selectorβ the context in which the truncation value is applied. The selectors are described in the online help, accessed from JShell using/help /set truncation.
π /types [option]
Displays classes, interfaces, and enums that were entered. No option entered? Then all current active classes, interfaces, and enums are displayed. Look at all your little creations π₯Ή
The following options are valid:
{name|id|startID-endID} [{name|id|startID-endID}...]β displays information for classes, interfaces, and enums identified by name, ID, or ID range. For a range of IDs, provide the starting ID and ending ID separated with a hyphen. To provide a list, separate the items in the list with a space. Use the/listcommand to see the IDs of the code snippets β that's your cheat sheet π₯-allβ displays information for all classes, interfaces, and enums, including those added when JShell was started, and classes, interfaces, and enums that failed, were overwritten, or were dropped. The whole messy history. Respect the journey π-startβ displays information for startup classes, interfaces, and enums that were added when JShell was started.
π /vars [option]
Displays the name, type, and value of variables that were entered. No option entered? All current active variables are displayed. Your variable collection, on full display β¨
The following options are valid:
{name|id|startID-endID} [{name|id|startID-endID}...]β displays information for variables identified by name, ID, or ID range. For a range of IDs, provide the starting ID and ending ID separated with a hyphen. To provide a list, separate the items in the list with a space. Use the/listcommand to see the IDs of the code snippets.-allβ displays information for all variables, including those added when JShell was started, and variables that failed, were overwritten, or were dropped. Yes, even the fallen soldiers π«‘-startβ displays information for startup variables that were added when JShell was started./?β literally just the same as the/helpcommand. π/!β reruns the last snippet. iconic./{name|id|startID-endID} [{name|id|startID-endID}...]β reruns the snippets identified by ID, range of IDs, or name. For a range of IDs, give the starting ID and ending ID separated with a hyphen. To give a list, separate the items in the list with a space. The first item in the list must be an ID or ID range. Use the/listcommand to see the IDs of the code snippets. π«Ά/-nβ reruns the-nthprevious snippet. For example, if 15 code snippets were entered, then/-4runs the 11th snippet. Commands aren't included in the count. bestie, the math is mathing.
β¨ INPUT SHORTCUTS β¨
The following shortcuts are available for entering commands and snippets in JShell.
π Tab completion
<tab>β 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.
Shift+<Tab> Vβ after entering a complete expression, use this key sequence to convert the expression to a variable declaration of a type determined by the type of the expression. πShift+<Tab> Mβ after entering a complete expression or statement, use this key sequence to convert the expression or statement to a method declaration. If an expression is entered, the return type is based on the type of the expression.Shift+<Tab> Iβ when an identifier is entered that can't be resolved, use this key sequence to show possible imports that resolve the identifier based on the content of the specified class path. no cap, this one is lowkey underrated.
πΈ Command abbreviations
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.
π¦ History navigation
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.
π§ History search
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.
π INPUT EDITING π
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.
β Line Editing Shortcuts
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β afterCtrl+Y, press to cycle through the previously deleted text.
π EXAMPLE OF STARTING AND STOPPING A JSHELL SESSION π
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
%
π EXAMPLE OF ENTERING SNIPPETS π
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
π EXAMPLE OF CHANGING SNIPPETS π
Change the definition of a variable, method, or class by entering it again.
The following example 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";
}
πΈ EXAMPLE OF CREATING A CUSTOM FEEDBACK MODE πΈ
Okay so, a feedback mode is basically the whole vibe of your JShell session: it decides the prompt you see, the feedback messages you get as snippets are entered, and the maximum length of a displayed value. There are already predefined feedback modes ready to go, and there are also commands for creating your own custom ones. Bestie, weβre about to make it bespoke. π
Use the /set mode command to create a new feedback mode. In the example below, the new mode mymode is based on the predefined feedback mode normal, and the command feedback is displayed so you can verify it:
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 at first. The next example shows how to see what prompts are being used, and then how to change them to custom strings. The first string is the standard JShell prompt. The second string is the prompt for additional lines in multiline snippets. Itβs giving customization era. β¨
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, plus 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 itβs 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$
π This page is from JDK 21, dated 2023, and itβs JSHELL(1) β now go make your terminal era iconic, bestie! π«Άβ¨
π¦ where this page comes from
- centos-stream 10: java-21-openjdk-devel 1:21.0.12.1.1-2.1.el10 Β· ASL 1.1 and ASL 2.0 and BSD and BSD with advertising and GPL+ and GPLv2 and GPLv2 with exceptions and IJG and LGPLv2+ and MIT and MPLv2.0 and Public Domain and W3C and zlib and ISC and FTL and RSA
- opensuse-leap 16.0: java-21-openjdk-devel 21.0.12.1-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openjdk-devel 21.0.12.1-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openjdk-devel 21.0.12.1-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- opensuse-leap 16.0: java-21-openjdk-devel 21.0.12.1-160000.1.1 Β· Apache-1.1 AND Apache-2.0 AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-only WITH Classpath-exception-2.0 AND LGPL-2.0-only AND MPL-1.0 AND MPL-1.1 AND LicenseRef-SUSE-Public-Domain AND W3C
- Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms