JSHELL(1)

๐Ÿ“ฆ version 17.0.20.1.1, in centos-stream 9

โœจ JSHELL(1) โœจ โ€” JDK Commands, but make it make sense ๐Ÿ’–

okay so this is the jshell manual page, bestie, and honestly it's a whole vibe ๐ŸŒˆ. grab a snack ๐Ÿ“, we're doing this.


๐ŸŒธ NAME ๐ŸŒธ

jshell โ€” it lets you interactively evaluate declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL) ๐Ÿ‘‘. like, you type Java, it yells back. iconic.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

jshell [options] [load-files]
  • options โ€” command-line options, separated by spaces. see Options for jshell below, bestie ๐Ÿ’…
  • 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 โ€” defines print, 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 ๐ŸŽฏ no cap, extremely useful.


๐Ÿฆ‹ 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 literally nothing, depending on the snippet entered and the feedback mode chosen ๐Ÿ™ˆ errors are described regardless of the feedback mode โ€” so you can't hide from your mistakes, bestie ๐Ÿ˜ญ 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 ๐Ÿ’พ periodt.


๐Ÿ’… 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 the CLASSPATH environment 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. know the difference, iykyk ๐Ÿ’…
  • --enable-preview โ€” allows code to depend on the preview features of this release ๐Ÿ‘€ (preview = spicy, handle with care)
  • --execution specification โ€” specifies an alternate execution engine, where specification is an ExecutionControl spec. see the documentation of the package jdk.jshell.spi for 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 mode command. the default is normal ๐ŸŽš๏ธ

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 ๐ŸŽจ
  • --help or -h or -? โ€” prints a summary of standard options and exits the tool ๐Ÿ™‹โ€โ™€๏ธ
  • --help-extra or -X โ€” prints a summary of nonstandard options and exits the tool. nonstandard options are subject to change without notice โš ๏ธ (so don't get attached, bestie)
  • -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 --startup option is used. pick one, girl ๐Ÿ’€
  • -q โ€” sets the feedback mode to concise, 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 to silent, 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 โ€” defines print, 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. again: pick one ๐Ÿ’€

  • -v โ€” sets the feedback mode to verbose, 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 /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:

  • {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 /list command 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 ๐Ÿ—‚๏ธ (the whole group chat, even the deleted messages)
  • -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:
    1. updates the environment settings with the provided options ๐Ÿ› ๏ธ
    2. resets the execution state ๐Ÿงผ
    3. runs the startup scripts ๐Ÿšฉ
    4. 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...] โ€” specifies the root modules to resolve in addition to the initial module ๐Ÿงฑ
  • --add-exports source-module/package=target-module[,target-module]* โ€” adds an export of package from source-module to target-module ๐Ÿš€
  • --class-path path โ€” this is where you tell JShell which directories and archives to search when it's hunting down class files. It overrides whatever's chillin' in the CLASSPATH environment variable. And if that env var isn't set and you don't use this option? It just searches the current directory, nbd. Path separator deets: on Linux and macOS use a colon (:), on Windows use a semicolon (;). ๐Ÿ’…
  • --module-path modulepath โ€” tells JShell where to find application modules. Same separator rules, bestie: colon (:) for Linux and macOS, semicolon (;) for Windows.

๐ŸŽ€ /exit [integer-expression-snippet]

Exits the tool. Periodt.

If you don't give it a snippet, the exit status is just zero โ€” squeaky clean. But if you do enter a snippet and the result of that snippet is an integer, that result becomes the exit status. Iconic, honestly.

Now the plot twist ๐Ÿ˜ญ โ€” if an error happens, or the result of the snippet isn't an integer, it prints an error and the tool stays active. You can't escape that easily, bestie. ๐Ÿ™ˆ

๐Ÿฆ‹ /history

Displays everything that was entered in this session. Your whole chaotic journey, laid bare. โœจ

๐ŸŒธ /help [command|subject]

Displays info about commands and subjects. If you don't enter any options, you get a summary of information for all commands plus a list of available subjects. Type in a valid command and boom โ€” expanded information for that command. Type in a valid subject and you get the tea on that subject. ๐Ÿซถ

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 โ€” and yes, that includes the ones from the startup scripts and the scripts that were entered on the command line when JShell was started. Full receipts. ๐Ÿงพ

โญ /list [option]

Displays a list of snippets and their IDs. No option? Then you get all the active snippets โ€” but heads up, startup snippets are not included in that default view. ๐Ÿ™ˆ

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, give the starting ID and ending ID separated with a hyphen. To provide a list, separate the items with a space.
  • -all โ€” displays all snippets, including startup snippets and snippets that failed, were overwritten, or were dropped. IDs that begin with s are startup snippets. IDs that begin with e are 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. With no option, you get the name, parameter types, and return type of all active methods. Chef's kiss. ๐Ÿ‘‘

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, give the starting ID and ending ID separated with a hyphen. For a list, separate the items with a space. Use the /list command 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 you 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 โ€” defines print, println, and printf as jshell methods for use within the 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 /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...] โ€” specifies the root modules to resolve in addition to the initial module.
  • --add-exports source-module/package=target-module[,target-module]* โ€” adds an export of package from source-module to target-module.
  • --class-path path โ€” specifies the directories and archives that are searched to locate class files. This option overrides the path in the CLASSPATH environment 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 (;).
  • --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 (;).
  • -quiet โ€” replays the valid history without displaying it. Errors are displayed though. ๐Ÿซฃ
  • -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 /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. It's giving time machine. โณ

๐Ÿ’€ /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. Gone. Poof. Not me crying over your unsaved scratch code ๐Ÿ˜ญ

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 of package from source-module to target-module.
  • --class-path path โ€” specifies the directories and archives that are searched to locate class files. This option overrides the path in the CLASSPATH environment 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 (;).
  • --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 (;).

๐Ÿงš /save [options] file

Saves snippets and commands to the file you specified. No options? Then the active snippets get saved. Simple, clean, 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 /list command 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, an empty file. it's giving minimalist ๐ŸŽ€)

๐Ÿ‘‘ /set [setting]

Sets configuration information โ€” the external editor, startup settings, and feedback mode all live here. This command is also how you create a custom feedback mode with your own customized prompt, format, and truncation values. Highkey powerful.

If no setting is entered, then the current setting for the editor, startup settings, and feedback mode are displayed. A little vibe check of your config. ๐Ÿ’…

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 โ€” sets the editor to the default editor provided with JShell. Heads up: this option can't be used if a command for starting an editor is entered. Pick a lane, bestie. ๐Ÿ™ˆ
  • -delete โ€” sets the editor back to whatever was in effect when the session started. If you use it with -retain, then the retained editor setting gets deleted and the editor is set to the first of these environment variables that's actually found: JSHELLEDITOR, VISUAL, or EDITOR. If none of the editor environment variables are set, then this option sets the editor to the default editor. ๐Ÿซถ
bestie, important: this option can't be used if a command for starting an editor is entered.
  • -retain โ€” saves the editor setting across sessions. If no other option or a command is entered, then the current setting is saved.
  • -wait โ€” prompts the user to indicate when editing is complete. Otherwise control returns to JShell when the editor exits. Use this option if the editor being used exits immediately, for example, when an edit window already exists. This option is valid only when a command for starting an editor is entered.

๐Ÿ’ฌ feedback [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" 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. 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. Only custom modes created with the /set mode command 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 is accessed from JShell using the /help /set format command.
  • "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, which is accessed from JShell using the /help /set format command.
  • selector โ€” specifies the context in which the response format is applied. The selectors are described in the online help, which is accessed from JShell using the /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 โ€” specifies the level of feedback displayed for commands when using the mode. This option is required when creating a feedback mode. Use -command to show information and verification feedback for commands. Use -quiet to show only essential feedback for commands, such as error messages.
  • -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 -retain option with this option. Predefined modes can't be deleted.
  • -retain โ€” saves the named feedback mode across sessions. The name of the mode to retain is required.

Configure 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:

  • mode โ€” specifies the feedback mode to which the prompts are applied. Only custom modes created with the /set mode command 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 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:

  • DEFAULT โ€” loads the default entries, which are commonly used as imports.
  • JAVASE โ€” imports all Java SE packages.
  • PRINTING โ€” defines print, println, and printf as jshell methods for use within the 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 -retain option 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. Only custom modes created with the /set mode command 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 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:

  • {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 /list command 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.
  • -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 /list command 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.
  • -start โ€” displays information for startup variables that were added when JShell was started.

๐Ÿ†˜ /?

Same as the /help command.

๐Ÿ” /!

Reruns the last snippet. because sometimes you just gotta hit it again, no cap ๐Ÿ’…

  • /{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 /list command to see the IDs of the code snippets.
  • /-n โ€” Reruns the -nth previous snippet. For example, if 15 code snippets were entered, then /-4 runs the 11th snippet. Commands aren't included in the count.

๐ŸŽ€ INPUT SHORTCUTS ๐ŸŽ€

okay so these are the lil shortcuts you can use when you're typing commands and snippets into JShell. they're giving convenience, fr fr ๐Ÿ’…

โœจ 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. slay, it's helping โœจ
  • 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. iykyk, signatures are lowkey iconic ๐Ÿซถ
  • 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. no cap, it's picky in a helpful way ๐Ÿ’–

  • 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. the way /s is out here causing chaos ๐Ÿ˜ญ
  • 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. main character energy: never lose the receipts ๐ŸŽ€

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 through later entries. scrolling history but make it a scavenger hunt ๐Ÿ”


๐Ÿ’… 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. don't panic if your keyboard's built different ๐Ÿ’–

  • 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. undo but make it fashion โœจ

๐ŸŒธ 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. entrance: made ๐Ÿ‘‘

              % 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. autoloaded imports? kinda iconic ngl ๐Ÿฆ‹

              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. goodbye bestie, it's been real ๐Ÿ˜ญ๐Ÿ’–

              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 semicolons are being handled for you, periodt ๐Ÿ’…

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. scratch variable said โ€œI got thisโ€ ๐Ÿซถ

              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. redefine and redecorate, honestly โœจ

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 era: activated ๐Ÿ’œ

              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 bestie, so feedback mode is lowkey the ~aesthetic~ of your whole jshell session. It's what decides:

  • the prompt that gets displayed
  • the feedback messages that show up as snippets are entered
  • and the maximum length of a displayed value

The good news? There are predefined feedback modes already there for you, and there are also commands for making your own custom ones. Main character energy: you can absolutely build one. ๐Ÿ’…


Start it off with the /set mode command to create a new feedback mode. In this little example below, the new mode mymode is based on the predefined feedback mode normal, and it's turning on verification of command feedback:

jshell> /set mode mymode normal -command
|  Created new feedback mode: mymode

Since the new mode is based on normal, the prompts come out the same. So the next example shows you how to peek at what prompts are being used, and then how to switch them over to custom strings. Heads up on what these two strings mean (iykyk):

  1. The first string = the standard JShell prompt.
  2. The second string = the prompt for additional lines in multiline snippets.
jshell> /set prompt mymode
|  /set prompt mymode "\njshell> " "   ...> "

jshell> /set prompt mymode "\nprompt$ " "   continue$ "

Now, the maximum length of a displayed value is controlled by the truncation setting. And here's the cute part: different types of values can have different lengths. ๐ŸŽ€ The example here sets an overall truncation value of 72, plus a truncation value of 500 specifically for variable value expressions:

jshell> /set truncation mymode 72

jshell> /set truncation mymode 500 varvalue

The feedback that shows up after snippets are entered? Controlled by the format setting, and it depends on two things: the type of snippet entered, and the action taken for that snippet. In the predefined mode normal, the string that gets created is what's 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

Then use the /set feedback command to actually start using the feedback mode you just made. Like, go use your creation, bestie ๐Ÿซถ 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$

Look at that. Defined. Slayed. Iconic. Periodt. ๐Ÿ‘‘


Honestly? Go make a feedback mode that matches your whole vibe. The prompts are yours now. ๐ŸŒˆ

That's the whole page, bestie! ๐Ÿ’– This one's from JDK 17, dated 2021 โ€” so go forth, run your snippets, and may your prompts always be cute. ๐Ÿฆ‹โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • centos-stream 9: java-17-openjdk-devel-fastdebug 1:17.0.20.1.1-1.2.el9 ยท 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms