Tuesday, June 28, 2016

JShell Commands

JShell has a great collection of command descriptions available from the '/help' command. However, sometimes the terse descriptions given in JShell itself are not quite enough. So, here is a somewhat complete list of JShell commands with descriptions for your viewing pleasure.

The JShell REPL commands work give you a way to manipulate failed, overwritten, dropped and start-up snippets and even other commands. All commands start with a forward slash (/).

For those new to JShell, snippets are little chunks of Java code. For a quick introduction to JShell, see my Java 9 REPL tutorial. Once you get accustomed to JShell, you will realize there is not much of a limit to what you can do with it.

/list

The /list command comes in five versions: '/list', '/list -start', '/list -all', '/list <name>', and '\list <id>'.

/list
Lists out the snippets in memory. If you loaded snippets from a file, those are also displayed. Each snippet is splayed next to its ID.

/list -start
Lists out normally hidden snippets that are provided for you by JShell by default. Each provided snippet is splayed next to its ID. Startup snippet ID's begin with an 's'.

/list -all
Lists any snippets you have added to JShell, any error or replaced snippets, and the default snippets. Each provided snippet is splayed next to its ID.

/list <name>
Lists the snippet whose name you provided. This may be a variable name or a method name. Each provided snippet is splayed next to its ID.

/list <id>
JShell lists the snippet whose ID you provided. Each provided snippet is splayed next to its ID.

/edit

/edit
Opens the current snippets in JShell with the JShell Edit Pad application. This is a simple editor that allows you to make modifications to the snippets, Accept them, Cancel unaccepted changes, and Exit the editor. While the JShell Edit Pad is open, the JShell refuses to accept new input.

The editor used by the /edit command can be changed by the '/set editor' command.

/edit <name>
Opens the named snippet in the preferred editor. The default editor is JShell Edit Pad.

/edit <id>
Opens the snippet with the given ID in the preferred editor. The default editor is JShell Edit Pad.

/drop

/drop <name>
Drop a snippet by name. For example, if you want to drop a String called myString use the command '/drop myString', and myString will no longer be available from JShell.

/drop <id> Drop a snippet by ID. To see the list of ID's associated with snippets currently active in JShell, use the /list command. Dropping a snippet with the ID of 42 would be done with the following command: '/drop 42'.

The /drop command must be followed by a name or ID. It cannot be used by itself.

/save

JShell's /save command requires a file path. It cannot be run without any arguments.

/save <file>
Save the commands and snippets currently active in JShell to the provided file path. The resulting file contains one  command or snippet per line.

/save -all <file>
Save all commands and snippets, including overwritten, start-up, and failed commands and snippets to the provided file path. The resulting file contains one  command or snippet per line.

/save -history <file>
Save all commands and snippets run since this session of JShell started to the provided file.

/save -start <file>
Save all commands and snippets that initialized this session of JShell to the provided path. This is a great command for creating a base start-up script to customize when creating a new default JShell start-up.

/open <file>

/open cannot be run without a file argument. /open runs each line of the file specified at the given file location as though it is a command or snippet. Tab completion works, so finding files to open isn't too painful.

/vars

/vars
Displays the current variables as list. The list is formatted as follows.
<type> <name> = <value>

/vars <name>
Displays the variable of the given name.

/vars <id>
Displays the variable with the given ID.

/vars -start
Lists all start-up variables. The list is formatted as follows.
<type> <name> = <value>

/vars -all
Lists all variables. The list is formatted as follows. 
<type> <name> = <value>

/methods

/methods
Displays the current methods as list. The list is formatted as follows.
<method name>(<argument 1>,<argument 2>,...)<return type>

/methods <name>
Displays the method of the given name.

/methods <id>
Displays the method with the given ID.

/methods -start
Lists all start-up methods. The list is formatted as follows.
<method name>(<argument 1>,<argument 2>,...)<return type>

/methods -all
Lists all methods. The list is formatted as follows.
<method name>(<argument 1>,<argument 2>,...)<return type>

/types

/types
Classes, interfaces, and enums are types. The /types command lists all of these that are currently active in JShell.

/types <name>
Lists the class, interface or enum with the given name that was created most recently.

/types <id>
Lists the class, interface or enum with the given ID.

/types -start
Lists all the classes, interfaces and enums available that were added when JShell started. By default, no types are added at start-up, so this will be an empty list unless you are using a modified start-up configuration. You can customize the start-up configuration by using the '/set start <file>' command.

/types -all
Lists all classes, interfaces and enums including dropped, overwritten and failed types. This also includes any default types added at start-up.

/imports

Lists the currently active JShell imports. This includes any that were added at start-up, and any imports added as snippets after start-up.

/exit

The /exit command takes you out of JShell. If you want to save your snippets from your session, try using the /save command first. You're history of commands is available from the next session of JShell. Just use the up arrow to see them.

/reset

Resets JShell to the default state after start-up code has been executed. In other words, if you have set a new set of startup snippets and commands, JShell is set to a state where those have executed and every other command and snippet is lost. Remember to save any work before executing /reset, if you do not want it lost.

/reload

/reload
Runs all valid commands and snippets from the current JShell session except for commands or snippets from before the last '/reset' or '/reload'.

/reload -restore
Runs all valid commands and snippets necessary to restore the last session of JShell.

/reload [-restore] -quiet
-quiet can be run with or without the -restore option. It does the same as '/reload' and '/reload -restore', except without all the visual noise of printing out what is being reloaded. You can use the commands '/reload -quiet' and '/reload -restore -quiet'.

/classpath <path>

Adds the given path to the current JShell environment's class path. This is really handy if you want to import classes from a third-party JAR that is not currently in the class path. For example, you might type '/classpath /my/path/My.jar' to add the My.jar to your class path.

/history

JShell keeps all the commands and snippets that you have typed in since starting JShell, and displays them when you type /history. If you want commands or snippets from previous times you launced JShell, then use the up arrow to see them.

/help

The /help command in JShell lists out the information about commands and topics JShell uses. For example, if you wanted to find out how to use the \list command, you would type in '/help /list' and the /list command along with its accepted parameters would be displayed.

If you just want a list of all possible commands and subjects /help has descriptions of, then just enter '/help' in JShell and hit enter. 

/set

JShell's /set command allow massive amounts of customization of the current JShell session, but that customization is not saved for future sessions if it is not included in the start-up or held with the /retain command.

/set editor <command> <arg1> <arg2> ...
Change the default snippet and command editor. Any editor available to your system can be set to your default editor.

/set start <file>
Sets the default start-up file to use for configuring your JShell.

/set feedback <mode>
Sets the current feedback mode. Valid feedback modes are 'normal', 'silent', 'concise', and 'verbose'. Verbose is probably the best feedback mode for students learning the Java language using JShell.

/set mode <mode> [<old-mode>] [-command | -quiet | -delete]
Create a custom feedback mode. A new mode may be created from a previously existing mode.

/set prompt <mode> "<prompt>" "<continuation-prompt>"
Set the prompt used for the given feedback mode. Prompts are highly configurable.

/set truncation <mode> <length> <selector> ...
If displayed prompts are getting too long to be useful, you can shorten them with the /truncation command. Truncation can be targeted at specific case selectors.

/set format <mode> <field> "<format>" <selector> ....
JShell's '/set format' allows for custom configuration of snippet event display for the provided feedback mode.

/retain

At first glance it may not be obvious what /retain provides that /set does not provide. The big difference is that the changes in /set go away between sessions of JShell, whereas the changes made by retain stick around when the JShell session ends. JShell "retains" the changes when a new session starts.

/retain editor [<command> <arg1> <arg2> ...]
Specifies the editor to use when editing commands and snippets. The <command> and <args> may or may not be needed depending on your OS and the editor you choose to use. It will be different for every editor. The editor change sticks around even when JShell restarts.

/retain start [<file>]
Specifies the start-up configuration of JShell. If no file is specified, the current start-up settings from the JShell session are held over to future sessions. If you don't want to use a file every time you start JShell, use the /set command to set the start-up commands and then use '/retain start' to set those start-up commands and snippets to your default start-up commands and snippets.

/retain feedback [<mode>]
Specifies the feedback mode to use in this and future sessions of JShell. When '/retain feedback' is given without the mode specified, the current feedback mode being used is the one that is retained for future sessions.

By specifying a feedback mode, the current feedback mode is changed to the specified feedback mode and retained for future JShell sessions. Valid feedback modes are 'normal', 'silent', 'concise', and 'verbose'. Verbose is probably the best feedback mode for students learning the Java language using JShell.

/retain mode <mode>
Used for retaining custom feedback modes. Custom feedback modes can be created using the /set command. Quite a bit of variety can be incorporated into custom feedback modes.

/?

The /? command is just a shortcut for /help. Everything that you can use /help for, you can substitute /? in its place.

/!

Do the last snippet again. This is a nice two character shortcut for those times that even tab completion is too annoying.

/<id>

Execute a snippet based off its ID. Dropped snippets cannot be executed.

/-<n>

Execute the n-th most recent snippet. This ignores commands for purposes of the count. So if you executed a snippet and then five commands, you would type '/-1' to execute the snippet.

Summary

Well, there you have it. A somewhat complete description of the JShell commands. Be sure to check out my other JShell tutorials.

No comments:

Post a Comment