Sub topic 5
Managing the session with slash commands
JShell treats a line that starts with / as a command to the tool itself, not as Java. Commands inspect what the session holds, remove snippets, save your work and reset state. This chapter assumes you can start JShell, enter expressions, and declare variables and methods as snippets.
Commands and snippets are different inputs
Everything you type is either a snippet or a command. A snippet is Java that JShell compiles and runs. A command begins with / and is handled by JShell without compiling anything. Commands never receive a scratch variable such as $1, and they are not recorded as snippets.
You can shorten any command to a prefix that is still unambiguous. Press Tab after a slash to see the available commands and complete them.
Inspecting what the session holds
Four commands answer the question "what have I defined so far?"
| Command | Shows |
|---|---|
| `/vars` | Variables, with their types and current values |
| `/methods` | Declared methods and their signatures |
| `/types` | Classes, interfaces, records and enums you declared |
| `/imports` | Active import declarations, including the defaults |
Here is a short session that uses them.
jshell> IntStream.rangeClosed(1, 5).sum()
$1 ==> 15
jshell> List<String> names = List.of("Ada", "Grace")
names ==> [Ada, Grace]
jshell> int square(int x) { return x * x; }
| created method square(int)
jshell> /vars
| int $1 = 15
| List<String> names = [Ada, Grace]
jshell> /methods
| int square(int)Scratch variables like $1 appear in /vars because JShell created real variables for them.
Reading the snippet history with /list
/list prints every active snippet with its numeric id. These are the snippets you can still act on.
jshell> /list
1 : IntStream.rangeClosed(1, 5).sum()
2 : List<String> names = List.of("Ada", "Grace");
3 : int square(int x) { return x * x; }The list leaves out two groups by default. /list -start shows the snippets from the startup script, which are the default imports. They have ids such as s1, s2. /list -all shows everything, including startup snippets and snippets that failed to compile. Failed snippets get ids such as e1. This helps when you want the text of an input that was rejected and need to see what you typed.
You can also pass a name or id to see one snippet: /list square or /list 3.
Changing the session
Removing a snippet with /drop
/drop takes a variable name, a method name or a snippet id.
jshell> /drop names
| dropped variable names
jshell> /methods
| int square(int)You can also drop by id, for example /drop 1, which removes the first snippet. This is useful for scratch variables, which have no name you chose. /drop $1 works too.
Dropping has consequences for anything that depends on the dropped snippet. If a method uses a variable you drop, the method becomes unresolved. JShell reports this and the method cannot be invoked until the variable is declared again. Chapter material on forward references covers this state in detail.
Starting over with /reset and /reload
/reset discards all snippets and returns the session to a clean state. The startup script runs again, so the default imports come back. Use it when the session has drifted and you no longer trust it.
/reload also restarts the execution state, but it then replays the valid history you typed. This is useful after you change a jar on the class path or want to confirm that your input still works from top to bottom. The replay re-runs your code, so any side effects such as printing or file writes happen again.
jshell> /reset
| Resetting state.
jshell> /vars
jshell>After a reset /vars prints nothing, because no variables exist.
Keeping your work
Saving with /save and loading with /open
/save file writes the source of the active snippets to a file, in the order you entered them. /open file reads a file and runs it as if you had typed its contents.
jshell> /save session.jsh
jshell> /reset
| Resetting state.
jshell> /open session.jsh
jshell> /methods
| int square(int)The default /save writes only snippets that are currently active. Options change this:
/save -all filewrites every snippet, including startup and failed ones./save -history filewrites everything you typed in order, commands included./save -start filewrites the current startup definitions.
The saved file is plain text with one snippet after another. You can edit it in any editor, and the scripts chapter covers running such files from the command line.
Looking back with /history
/history prints every line you entered in this session, including commands and input that failed. It differs from /list. /list shows only snippets that remain active and numbers them. /history shows raw input in the order you typed it.
Repeating input
JShell has shortcuts for running earlier input again:
/!reruns the last snippet./-2reruns the snippet two back from the latest./-1is the same as/!./3reruns the snippet with id 3.
Rerunning a variable declaration assigns a fresh value to that variable. For example, rerunning int counter = 0; resets the counter. Rerunning a snippet creates a new execution but keeps its original id when the snippet is a redefinition of an existing declaration.
Getting help and leaving
/help lists all commands with a one-line description. /help /list explains one command in detail. /help intro gives a short introduction to the tool, and /help shortcuts lists the keyboard shortcuts. /exit ends the session. Pressing Ctrl+D also ends it.
/exit accepts an optional integer expression, which becomes the exit code of the jshell process. This is useful in scripts.
int failures = 0;
/exit failuresIf you save this as check.jsh and run jshell check.jsh, the process exits with status 0 because failures is 0.
Common errors
- Using a command name that is ambiguous. A prefix such as
/rematches both/resetand/reload, so JShell asks you to be more specific. - Dropping a snippet and expecting dependents to keep working. Dependent methods become unresolved until you redeclare what they need.
- Trusting a long session. After many redefinitions, state can differ from what a clean top-to-bottom run produces. Run
/save,/resetand/opento check that the saved file works on its own. - Forgetting that
/reloadre-executes code. Side effects such as file writes happen again. - Expecting
/listto show failed input. Use/list -alland look for ids that start withe.
Exercise
You defined a variable total, a method add(int, int), and then made a typing mistake in a third snippet that failed to compile. You want to (1) see the text of the failed snippet, (2) remove total, and (3) keep a file with everything you typed, including the commands. Which commands do you use?
Show answer
Use /list -all to see the failed snippet, which has an id starting with e. Use /drop total to remove the variable. Use /save -history session.txt to save everything you typed, including commands. Plain /save would write only the active snippets and would leave out the commands and the failed input.
Exercise
What is the difference between /reset and /reload, and when would you choose /reload?
Show answer
/reset clears all snippets and gives you an empty session, apart from the startup script. /reload restarts the execution state and then replays your valid history, so your definitions come back. Choose /reload when you changed something outside the session, such as a jar on the class path, and want your earlier snippets to run again against it. Remember that the replay re-executes the code and repeats any side effects.