JSHELL(1)

πŸ“¦ version 22.0.2.0, in opensuse-leap 16.0

πŸ’… jshell β€” JDK Commands πŸ’…

okay so bestie, welcome to the JSHELL(1) page β€” yes, JSHELL(1) is the manual name, and we are about to make it make sense. This is the JDK command that gives you a read-eval-print loop, aka a REPL, for the Java programming language. iconic behavior.

🌸 NAME 🌸

jshell β€” interactively evaluate declarations, statements, and expressions of the Java programming language in a read-eval-print loop (REPL). no cap, that's the whole vibe.

✨ 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 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.
  • TOOLING β€” Defines javac, 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 /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. cute AND 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. love that for us.

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. so organized of you.

πŸ’– OPTIONS FOR JSHELL πŸ’–

  • --add-exports module/package β€” Specifies a package to be considered as exported from its defining module. slay.
  • --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. path drama, but make it precise.
  • --enable-preview β€” Allows code to depend on the preview features of this release. live a little, but carefully.
  • --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. bye bestie.
  • --help-extra or -X β€” Prints a summary of nonstandard options and exits the tool. Nonstandard options are subject to change without notice. ⚠️ read that again.
  • -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 a lane, gorgeous.
  • -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.
  • TOOLING β€” Defines javac, 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 /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 a lane.
  • -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. main character command energy only.

  • /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.
  • -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. bless the fallback.

  • /env [options] β€” Displays the environment settings, or updates the environment settings and restarts the session. If no option is entered, then the current environment settings are displayed. If one or more options are entered, then the session is restarted as follows:
    • Updates the environment settings with the provided options.
    • Resets the execution state.
    • Runs the startup scripts.
    • Silently replays the history in the order entered. The history includes all valid snippets or /drop commands entered at the jshell prompt, in scripts entered on the command line, or scripts entered with the /open command.

Environment settings entered on the command line or provided with a previous /reset, /env, or /reload command are maintained unless an option is entered that overwrites the setting.

The following options are valid:

  • --add-modules module[,module...] β€” tells the tool which root modules to resolve on top of the initial module 🌸
  • --add-exports source-module/package=target-module[,target-module]* β€” adds an export of package from source-module over to target-module ✨
  • --class-path path β€” where should we go hunting for class files? These directories and archives, bestie πŸ’… this option overrides whatever's sitting in the CLASSPATH environment variable. And if that env var isn't set and you didn't pass this option? Then the current directory gets searched. On Linux and macOS, separate items in the path with a colon (:). On Windows, use a semicolon (;) 🫢
  • --module-path modulepath β€” where to find application modules πŸ¦‹ Linux and macOS: colon (:) between items. Windows: semicolon (;) between items. That's the whole vibe check.

  • /exit [integer-expression-snippet] β€” peaces out of the tool πŸ‘‹ no snippet entered? Exit status is zero. Entered a snippet and its result is an integer? That result becomes the exit status, iconic. But if an error happens, or the snippet's result isn't an integer β€” an error gets displayed and the tool stays active. Not me getting refused at the door 😭
  • /history β€” shows you everything that was entered in this session πŸ“œ a whole recap of your main character era.
  • /help [command|subject] β€” your built-in bestie for info about commands and subjects πŸ’– no options entered? You get a summary of info for all commands plus a list of available subjects. Pass a valid command? Expanded info for that command, slay. Pass a valid subject? Info about that subject. Periodt.

These are the valid values for subject:

  • context β€” describes the options available for configuring the environment 🌈
  • intro β€” gives you an introduction to the tool ✨
  • shortcuts β€” describes the keystrokes for completing commands and snippets. See Input Shortcuts πŸŽ€
  • /imports β€” displays the current active imports, and that includes the ones from startup scripts and scripts entered on the command line when JShell was started πŸ‘‘ nothing gets left out.
  • /list [option] β€” shows a list of snippets and their IDs πŸ“‹ with no option entered you get all active snippets β€” but startup snippets are not included, okurr?

These 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. For 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] β€” info about the methods you entered 🧚 no option entered? You see the name, parameter types, and return type of all active methods.

These options are valid:

  • {name|id|startID-endID} [{name|id|startID-endID}...] β€” displays info for methods identified by name, ID, or ID range. For a range of IDs, 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 info for all methods, including those added when JShell was started, and methods that failed, were overwritten, or were dropped πŸ”₯
  • -start β€” displays info 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 the predefined scripts below:
    • 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 πŸ–¨οΈ
    • TOOLING β€” defines javac, jar, and other methods for running JDK tools via their command-line interface within the jshell tool πŸ› οΈ
  • /reload [options] β€” restarts the session, like this:
    • 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 🫢

These 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. Linux and macOS: colon (:) to separate items. Windows: semicolon (;) πŸ’…
  • --module-path modulepath β€” specifies where to find application modules. Linux and macOS: colon (:) to separate items. Windows: semicolon (;) πŸ¦‹
  • -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 /env command was executed in the previous run. The valid history since that point is replayed. Use this option to restore a previous JShell session πŸ’–
  • /reset [options] β€” discards all entered snippets and restarts the session, like this:
    • 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 😭 no cap, it's gone gone.

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 🫢

These 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. Linux and macOS: colon (:). Windows: semicolon (;) πŸ’…
  • --module-path modulepath β€” specifies where to find application modules. Linux and macOS: colon (:). Windows: semicolon (;) πŸ¦‹
  • /save [options] file β€” saves snippets and commands to the file you specified πŸ’Ύ with no options entered, active snippets get saved.

These 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, 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 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 info, including the external editor, startup settings, and feedback mode πŸ‘‘ this command is also how you 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 ✨

These are the valid values 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 your editor to the default editor that ships with JShell πŸ’–. Heads up though: you can't use this if you've entered a command for starting an editor.
  • -delete β€” resets the editor to whatever was in effect when the session started. Ngl, the tea here: if you pair it with -retain, the retained editor setting gets deleted and the editor is set to the first of these environment variables it finds: JSHELLEDITOR, VISUAL, or EDITOR. And if none of those editor environment variables are set? Then this option just sets the editor to the default editor. Same rule as above applies β€” can't be used if you've entered a command for starting an editor πŸ™ˆ.
  • -retain β€” saves your editor setting across sessions, so it sticks around like a good habit ✨. If no other option or command is entered, the current setting is what gets saved.
  • -wait β€” prompts you to say when editing is done. Otherwise, control goes back to JShell the moment the editor exits. Use this one if your editor bails immediately, like when an edit window already exists πŸ‘€. This option is valid only when you enter a command for starting an editor.

πŸŽ€ feedback [mode] πŸŽ€

Sets the feedback mode that's used to respond to your input. If you don't enter a mode, you get shown the current mode β€” vibe check first, always πŸ’….

The valid modes are: concise, normal, silent, verbose, and any custom mode you made with the /set mode command. Iconic lineup, honestly.

🌸 format mode field "format-string" selector 🌸

Sets the format of the feedback you get in response to input. The no-args lore is very generous:

  • No mode entered? You get the current formats for all fields for all feedback modes.
  • Only a mode entered? You get the current formats for that mode.
  • Only a mode and field entered? You get the current formats for that field.

To actually define a format, these arguments are required:

  • mode β€” specifies the feedback mode the response format applies to. Only custom modes created with the /set mode command can be modified. No touching the built-ins, bestie.
  • field β€” specifies the context-specific field the response format applies to. The fields are described in the online help, which you get to from JShell with /help /set format.
  • "format-string" β€” the string to use as the response format for the specified field and selector. The structure of that 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 also described in the online help, accessed from JShell with /help /set format.

πŸ¦‹ mode [mode-name] [existing-mode] [options] πŸ¦‹

Creates a custom feedback mode with the mode name you give it. If you don't enter a mode name, you get the settings for all modes displayed β€” which includes the mode, prompt, format, and truncation settings. If you do name an existing mode, its settings get copied over to the mode you're creating. Hand-me-downs but make it character development πŸ’œ.

The following options are valid:

  • -command|-quiet β€” specifies the level of feedback shown for commands when you're 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 β€” like error messages, the bare minimum 😭.
  • -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 together with this one. Predefined modes can't be deleted, sorry bestie, they're permanent residents.
  • -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 truncation commands. Then, to actually start using the new mode, use the /set feedback command. Slay, you're a mode designer now πŸ’…βœ¨.

🌈 prompt mode "prompt-string" "continuation-prompt-string" 🌈

Sets the prompts for input inside JShell. If you don't enter a mode, you get the current prompts for all feedback modes displayed. If you enter just a mode, you get the current prompts for that mode. Cheap little preview, love that for you 🫢.

To define a prompt, these arguments are required:

  • mode β€” specifies the feedback mode the prompts apply to. Only custom modes created with the /set mode command 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.

⭐ start [-retain] [file [file...]|option] ⭐

Sets the names of the startup scripts used the next time a /reset, /reload, or /env command is entered. If you enter more than one script, they run in the order you entered them. If you enter no scripts or options at all, the current startup settings get displayed.

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. All of them. Full sweep πŸ‘‘.
  • PRINTING β€” defines print, println, and printf as jshell methods for use within the tool.
  • TOOLING β€” defines javac, 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. Nothing, nada, empty vibes.

Use the -retain option to save the start setting across sessions πŸ’–.

πŸ“ truncation mode length selector πŸ“

Sets the maximum length of a displayed value. If you don't enter a mode, the current truncation values for all feedback modes get displayed. If you enter just a mode, you get the current truncation values for that mode. Short and sweet, literally.

To define truncation values, these arguments are required:

  • mode β€” specifies the feedback mode the truncation value applies to. 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 you access from JShell using /help /set truncation.

🧚 /types [option] 🧚

Displays the classes, interfaces, and enums that were entered. If you don't enter an option, then all current active classes, interfaces, and enums are displayed. Everyone's invited to the party πŸ’œ.

The following options are valid:

  • {name|id|startID-endID} [{name|id|startID-endID}...] β€” displays information for the classes, interfaces, and enums identified by name, ID, or ID range. For a range of IDs, give the starting ID and ending ID separated with a hyphen. To hand over a list, separate the items with a space. Use the /list command to see the IDs of the code snippets β€” that's where the receipts live, iykyk.
  • -all β€” displays information for all classes, interfaces, and enums, including the ones added when JShell was started, plus classes, interfaces, and enums that failed, were overwritten, or were dropped. The whole lore, the flops included πŸ’€.
  • -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 the variables that were entered. If you don't enter an option, all current active variables are displayed.

The following options are valid:

  • {name|id|startID-endID} [{name|id|startID-endID}...] β€” displays information for the variables 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. Use the /list command to see the IDs of the code snippets.
  • -all β€” displays information for all variables, including the ones added when JShell was started, plus variables that failed, were overwritten, or were dropped. No variable left behind 🌸.
  • -start β€” displays information for startup variables that were added when JShell was started.
  • /? β€” Same as the /help command. πŸ’…
  • /! β€” Reruns the last snippet. Periodt.
  • /{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 ✨

The following shortcuts are available for entering commands and snippets in JShell. Okay bestie, these are gonna save you so much typing. 🫢

πŸ¦‹ 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. 🌈

πŸ’… 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. iykyk. πŸ’…

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. πŸŽ€

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. No cap, this is your little keyboard cheat sheet. πŸ’œ

πŸ’œ Line Editing Shortcuts πŸ’œ

Key or Key Combination β†’ Action

  • Return β€” Enter the current line.
  • Left arrow β€” Move the cursor to the left one character.
  • Right arrow β€” Move the cursor to the right one character.
  • Ctrl+A β€” Move the cursor to the beginning of the line.
  • Ctrl+E β€” Move the cursor to the end of the line.
  • Meta+B β€” Move the cursor to the left one word.
  • Meta+F β€” Move the cursor to the right one word.
  • Delete β€” Delete the character under the cursor.
  • Backspace β€” Delete the character before the cursor.
  • Ctrl+K β€” Delete the text from the cursor to the end of the line.
  • Meta+D β€” Delete the text from the cursor to the end of the word.
  • Ctrl+W β€” Delete the text from the cursor to the previous white space.
  • Ctrl+Y β€” Paste the most recently deleted text into the line.
  • Meta+Y β€” After Ctrl+Y, press to cycle through the previously deleted text.

🌈 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 energy fr fr. πŸ‘‘

% 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! 😭

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. Not me loving auto-semicolons for us. πŸŽ€

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. Literally just run it back. πŸ”₯

The following examples shows a method being defined and the method run:

jshell> String grade(int testScore) {
   ...>     if (testScore >= 90) {
   ...>         return "Pass";
   ...>     }
   ...>     return "Fail";
   ...> }
|  created method grade(int)

jshell> grade(88)
$3 ==> "Fail"

To change the method grade to allow more students to pass, enter the method definition again and change the pass score to 80. Use the up arrow key to retrieve the previous entries to avoid having to reenter them and make the change in the if statement. The following example shows the new definition and reruns the method to show the new result: πŸ’–

jshell> String grade(int testScore) {
   ...>     if (testScore >= 80) {
   ...>         return "Pass";
   ...>     }
   ...>     return "Fail";
   ...> }
|  modified method grade(int)

jshell> grade(88)
$5 ==> "Pass"

For snippets that are more than a few lines long, or to make more than a few changes, use the /edit command to open the snippet in an editor. After the changes are complete, close the edit window to return control to the JShell session. The following example shows the command and the feedback provided when the edit window is closed. The /list command is used to show that the pass score was changed to 85. πŸ¦‹

jshell> /edit grade
|  modified method grade(int)
jshell> /list grade

   6 : String grade(int testScore) {
           if (testScore >= 85) {
               return "Pass";
           }
           return "Fail";
       }

🌸 EXAMPLE OF CREATING A CUSTOM FEEDBACK MODE 🌸

okay bestie, so the feedback mode is literally the vibe-setter: it decides the prompt that’s displayed, the feedback messages that are provided as snippets are entered, and the maximum length of a displayed value. Predefined feedback modes are provided, and commands for creating custom feedback modes are also provided. slay.

Use the /set mode command to create a new feedback mode. In the following example, the new mode mymode is based on the predefined feedback mode, normal, and verifying command feedback is displayed:

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

Because the new mode is based on the normal mode, the prompts are the same. The following example shows how to see what prompts are used and then changes the prompts to custom strings. The first string represents the standard JShell prompt. The second string represents the prompt for additional lines in multiline snippets.

jshell> /set prompt mymode
|  /set prompt mymode "\njshell> " "   ...> "

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

The maximum length of a displayed value is controlled by the truncation setting. Different types of values can have different lengths. The following example sets an overall truncation value of 72, and a truncation value of 500 for variable value expressions:

jshell> /set truncation mymode 72

jshell> /set truncation mymode 500 varvalue

The feedback displayed after snippets are entered is controlled by the format setting and is based on the type of snippet entered and the action taken for that snippet. In the predefined mode normal, the string created is displayed when a method is created. The following example shows how to change that string to defined:

jshell> /set format mymode action "defined" added-primary

Use the /set feedback command to start using the feedback mode that was just created. The following example shows the custom mode in use:

jshell> /set feedback mymode
|  Feedback mode: mymode

prompt$ int square (int num1){
   continue$ return num1*num1;
   continue$ }
|  defined method square(int)

prompt$

okay that’s the whole page, bestie β€” JDK 22 energy, dated 2024, and this one is JSHELL(1), periodt πŸ’…βœ¨

πŸ“¦ where this page comes from