JSHELL(1)
π JSHELL(1) π
okay so real talk β this is the JSHELL(1) page from the JDK Commands part of the manual, and it's kind of a lot, but we're gonna get through it together bestie π grab a snack, we're learning jshell.
β¨ NAME β¨
jshell β it lets you interactively evaluate declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL). yes, a REPL. it's giving "type stuff, immediately see what happens" energy and honestly? iconic π
πΈ SYNOPSIS πΈ
jshell [options] [load-files]
optionsβ command-line options, separated by spaces. see Options for jshell down below π«Ά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, 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. 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/opencommand. 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 all accepted. the little bits of code you enter are called snippets π
as snippets are entered, they're evaluated, and feedback is provided. feedback varies from the results and explanations of actions to literally nothing, depending on the snippet entered and the feedback mode chosen. errors are described regardless of the feedback mode β no cap, you always get to know when you messed up π 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-exportsmodule/package β specifies a package to be considered as exported from its defining module.--add-modulesmodule[,module...] β specifies the root modules to resolve in addition to the initial module.-Cflagβ passes flag to the Java compiler inside JShell. for example,-C-Xlintenables all the recommended lint warnings, and-C--release=<N>compiles for Java SE N, as if--release Nwas specified.--class-pathpath β 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 π--executionspecification β specifies an alternate execution engine, where specification is anExecutionControlspec. see the documentation of the packagejdk.jshell.spifor the syntax of the spec.--feedbackmode β 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 feedbackmode command. 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 modecommand.
--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β passes flag to the runtime system, but has no effect on the execution of code snippets. to specify flags that affect the execution of code snippets, use-Rflag. alternatively, use-Jflagwith--execution local.--module-pathmodulepath β 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β passes flag to the runtime system only when code snippets are executed. for example,-R-Dfoo=barmeans that execution of the snippetSystem.getProperty("foo")will return"bar"β¨-sβ sets the feedback mode tosilent, which is the same as entering--feedback silent.--show-versionβ prints version information and enters the tool.--startupfile β 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, 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. 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/envcommand. 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-startupoption 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-waitoption was used when the editor was set. use the/set editorcommand to specify the editor to use. if no editor is set, then the following environment variables are checked in order:JSHELLEDITOR,VISUAL, andEDITOR. 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/reloadcommand are maintained unless an option is entered that overwrites the setting. The following options are valid: --add-modulesmodule[,module...]β specifies the root modules to resolve in addition to the initial module.--add-exportssource-module/package=target-module[,target-module]*β adds an export ofpackagefrom source-module to target-module.--class-pathpathβ 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-pathmodulepathβ 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.
π
/exit [integer-expression-snippet]
Bestie, this is the door out. It exits the tool. No snippet entered? Exit status is zero, easy peasy. But if you do enter a snippet and its result is an integer, that result becomes your exit status β like, the exit code is literally whatever your little expression says π. And if an error happens, or the result of the snippet is not an integer? An error is displayed and the tool remains active, so no dramatic escape, you're still here π.
πΈ /history
Displays what was entered in this session. That's it, that's the vibe check on your own chaos β¨.
π /help [command|subject]
Displays information about commands and subjects. No options entered? Then you get a summary of information for all commands plus a list of available subjects β a whole menu, iconic π. 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:
contextβ describes the options that are available for configuring the environment.introβ provides an introduction to the tool.shortcutsβ describes keystrokes for completing commands and snippets. See Input Shortcuts.
π¦ /imports
Displays the current active imports, including those from the startup scripts and scripts that were entered on the command line when JShell was started. Lowkey the receipts of everything you pulled in π.
β /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 β sneaky, right? π
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.-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 the name, parameter types, and return type of all active methods are displayed. Casual little method parade π«.
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 the following 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 thejshelltool.
π₯ /reload [options]
Restarts the session, and here's the whole rundown:
- 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 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/reloadcommand are maintained unless an option is entered that overwrites the setting.
The following options are valid:
--add-modulesmodule[,module...]β specifies the root modules to resolve in addition to the initial module.--add-exportssource-module/package=target-module[,target-module]*β adds an export ofpackagefrom source-module to target-module.--class-pathpathβ 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-pathmodulepathβ 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. Sneaky-but-useful energy π€«.-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 β literally the "wait, bring back my stuff" button ππ«Ά.
π /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.
β οΈ Okay bestie, read that again: all code that was entered is lost. No cap. /reset does not care about your feelings.
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-modulesmodule[,module...]β specifies the root modules to resolve in addition to the initial module.--add-exportssource-module/package=target-module[,target-module]*β adds an export ofpackagefrom source-module to target-module.--class-pathpathβ 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-pathmodulepathβ 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. If no options are entered, then active snippets are saved. Preservation, slay πΎβ¨.
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.
π /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. Main character customization menu, honestly π.
The following values are valid for setting:
π editor [options] [command]
Okay so this one sets the command that launches your external editor whenever you type /edit π
The command can totally include command arguments, just separate them with spaces. And if you enter no command and no options? It just shows you the current setting, like a little mirror moment β¨
The following options are valid:
-defaultβ sets the editor to the default editor that ships with JShell π«Ά You can't use this option if you're also entering a command for starting an editor. Pick a lane bestie.-deleteβ sets the editor back to whatever was in effect when the session started. If you use it with the-retainoption, then the retained editor setting gets deleted and the editor is set to the first of these environment variables it finds:JSHELLEDITOR,VISUAL, orEDITORπ If none of those editor env vars are set, then this option sets the editor to the default editor. Also: can't be used if you're entering a command for starting an editor. Same rule, sorry not sorry π-retainβ saves your editor setting across sessions, so it survives the reboot era π If no other option or command is entered, then the current setting is what gets saved.-waitβ prompts the user to indicate when editing is complete. Otherwise control returns to JShell when the editor exits. Use this one if your editor exits immediately, like when an edit window already exists π Lowkey this option is only valid when a command for starting an editor is entered.
π feedback [mode]
Sets the feedback mode used to respond to input β¨ If you enter no mode, then the current mode is displayed β surprise, it's a reveal.
The following modes are valid: concise, normal, silent, verbose, and any custom mode you created with the /set mode command π¦
πΈ format mode field "format-string" selector
Sets 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 (yes, an absolute info dump, we love her). 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:
modeβ specifies a feedback mode to which the response format is applied. Heads up: only custom modes created with the/set modecommand can be modified πfieldβ specifies a context-specific field to which the response format is applied. The fields are described in the online help, which you get to from JShell using the/help /set formatcommand."format-string"β specifies 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, accessed from JShell using/help /set format.selectorβ specifies the context in which the response format is applied. Selectors are described in the online help too, also via/help /set format.
Not me sending you to the online help four times in one paragraph π but fr fr that's where the deets live.
π¦ mode [mode-name] [existing-mode] [options]
Creates a custom feedback mode with the mode name you provide π If no mode name is entered, then the settings for all modes are displayed, which includes the mode, prompt, format, and truncation settings β the whole aesthetic. If you provide the name of an existing mode, then the settings from that existing mode get copied to the mode being created. Remix culture π
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. Use-commandto show information and verification feedback for commands. Use-quietto show only essential feedback for commands, such as error messages. Pick your vibe wisely β¨-deleteβ deletes the named feedback mode for this session. The name of the mode to delete is required. To permanently delete a retained mode, use the-retainoption together with this option. Predefined modes can't be deleted, they're protected icons π-retainβ saves the named feedback mode across sessions. The name of the mode to retain is required. Configure your new feedback mode using the/set prompt,/set format, and/set truncationcommands. Then to actually start using the new mode, use the/set feedbackcommand. Slay in an orderly fashion π
β 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:
modeβ specifies the feedback mode to which the prompts are applied. Again: only custom modes created with the/set modecommand can be modified π«Ά"prompt-string"β specifies the string to use as the prompt for the first line of input."continuation-prompt-string"β specifies the string to use as the prompt for the additional input lines needed to complete a snippet π¦
π 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 run in the order entered β first come, first served, no cutting the line.
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:
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. Use the-retainoption to save the start setting across sessions π
π truncation mode length selector
Sets 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:
modeβ specifies the feedback mode to which the truncation value is applied. You already know the drill: only custom modes created with the/set modecommand can be modified πlengthβ specifies the unsigned integer to use as the maximum length for the specified selector.selectorβ specifies the context in which the truncation value is applied. The selectors are described in the online help, which is accessed from JShell using the/help /set truncationcommand π
π«Ά /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. A full lineup, main character energy only.
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 π-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 π yes even the flops get a mention.-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 β¨ If no option is entered, then all current active variables are displayed.
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 π¦
okay so we're still in the command-option zone, and these are the rest of the little flags β no skipping, bestie, every single one is here:
-allβ Displays information for all variables, including those added when JShell was started, and variables that failed, were overwritten, or were dropped.-startβ Displays information for startup variables that were added when JShell was started./?β Same as the/helpcommand./!β Reruns the last snippet./{name|id|startID-endID} [{name|id|startID-endID}...]β Reruns the snippets identified by ID, range of IDs, or name. 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. 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.
β¨ INPUT SHORTCUTS β¨
The following shortcuts are available for entering commands and snippets in JShell. tiny keyboard magic, huge time save π
β¨ 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. literally the autocomplete fairy godmother π§
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.
β¨ 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. short and sweet, but still unambiguous π
β¨ 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. muscle-memory era, fr fr π¦
β¨ 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. main character entrance, honestly π
% 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. bye bestie, slay responsibly π
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. the $5 is giving βsurprise, I brought a variableβ π
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 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. editor window said βI gotchu,β and it did π«Ά
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 π
ok so before we build one, bestie, let's vibes-check what a feedback mode even is β because it's doing a lot of heavy lifting behind the scenes. A feedback mode decides three things: the prompt that gets displayed, the feedback messages that show up as you enter snippets, and the maximum length of a displayed value. Predefined feedback modes come included, and yeah β there are also commands for creating your own custom ones. slay. π
Use the /set mode command to create a new feedback mode. In this next 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 our new mode is based on normal, the prompts are literally the same. The example below shows how to peek at what prompts are in use, and then how to swap them out for custom strings. Heads up on what those two strings mean: the first string represents the standard JShell prompt, and 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, and different types of values can have different lengths β iconic, honestly. 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 plus the action taken for that snippet. So in the predefined mode normal, the string created gets displayed when a method is created. The example below shows how to change that string to defined:
jshell> /set format mymode action "defined" added-primary
Then use the /set feedback command to actually start using the feedback mode you just created β no cap, that's the step where it goes live. 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$
and that's a wrap on the whole page, bestie!! π«Ά you literally made it to the end, main character energy fr fr π this was JDK 27.0.1.0.3, dated 2026 β go forth and jshell responsibly, mwah πβ¨
π¦ where this page comes from
- arch rolling: jdk-openjdk 27.0.1.0.3.u1-1 Β· LicenseRef-Java
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms