What are JShell commands like /list, /vars, /edit, /save, /open, and how do you load or save scripts?
answer
- `/`-prefixed = command to JShell, not Java
- /list, /vars, /methods, /types = inspect state
- /edit fixes snippets; /drop removes; /reset clears all
- /save writes snippets; /open runs a script file
- Tab completion + /help for discovery; `jshell file.jsh` runs a script
basics
~20 sJShell commands start with a slash and control the session rather than running Java. For example /list shows your snippets, /vars lists variables, /edit opens an editor, /save file writes snippets to a file, and /open file runs a script of snippets.
solid answer
~50 sIn JShell, anything starting with `/` is a **command** to the tool (not Java code). The common ones: `/list` prints the snippets you've entered (with ids); `/vars`, `/methods`, and `/types` list the variables, methods, and classes currently defined; `/edit` opens a snippet in an editor pad so you can fix multi-line code; `/drop` removes a snippet; `/reset` clears the whole session; `/save <file>` writes your snippets to a file; and `/open <file>` reads and executes a file of snippets—how you load scripts. Tab completion helps discover both commands and Java APIs. This command set turns JShell from a throwaway prompt into a workable scratchpad: you can build up snippets, save them, reload them in a fresh session, or hand them to a teammate. `/help` (or `/?`) lists everything, and most commands accept arguments or snippet ids.
go deeper
Knows commands start with / and can use /list and /exit.
Can use the core command set to inspect (/vars, /methods), edit (/edit, /drop), and persist (/save, /open) a session, and load a script.
Builds a reproducible workflow with saved .jsh scripts, uses tab completion for API discovery, and knows the /save -all/-history and startup-script options.
Standardizes shared JShell scripts/startup files for onboarding or demos and weighs command-driven sessions against scripted, version-controlled alternatives for reproducibility.
## Two kinds of input: snippets vs. commands At the `jshell>` prompt you can type two different things: - **Snippets** — Java code (expressions, statements, declarations). These get evaluated. - **Commands** — instructions to JShell itself, always beginning with a **forward slash `/`**. These manage the session; they are not Java. This split lets JShell tell "run this code" apart from "do this to the session." ## The essential commands ### Inspecting state - **`/list`** — prints all the snippets you've entered so far, each with a numeric **id**. Add `-all` to include startup/overwritten snippets. - **`/vars`** — lists all currently defined variables and their values. - **`/methods`** — lists defined methods and their signatures. - **`/types`** — lists defined classes/interfaces/enums. ### Editing and removing - **`/edit`** — opens a snippet (or all snippets) in an **edit pad** (a small editor window, or your configured external editor). Handy for fixing multi-line methods you can't easily retype. - **`/drop <id-or-name>`** — removes a specific snippet from the session, as if you never typed it. - **`/reset`** — wipes the entire session back to empty (all variables, methods, types gone). ### Persistence — saving and loading scripts A **script** here just means a text file containing JShell snippets/commands, one workflow after another. - **`/save <file>`** — writes your current snippets to `<file>`. Variants: `/save -all` (everything including overwritten), `/save -history` (your raw typed history). - **`/open <file>`** — reads `<file>` and **executes** each line as if you typed it. This is how you **load a script**: define a set of helpers/variables in a file, then `/open` it at the start of a session. You can also pass a script on the command line: `jshell myscript.jsh`. ### Help and discovery - **`/help`** (or **`/?`**) — lists all commands and topics; `/help <command>` explains one. - **Tab completion** — pressing **Tab** completes command names, method names, variable names, and even shows method signatures/Javadoc, which makes exploring an unfamiliar API fast. ## A typical workflow 1. Experiment: type snippets, use `/list` and `/vars` to see state. 2. Fix a clumsy method with `/edit`. 3. Once it works, `/save work.jsh` to keep it. 4. Next session, `jshell` then `/open work.jsh` to restore your helpers and continue. ## Why the commands matter Without them, JShell would be a stateless toy. With them you can **inspect** what you've built, **repair** snippets, **persist** useful work, and **replay** it—turning the REPL into a lightweight but genuinely usable experimentation environment, and a convenient way to share runnable snippets (a `.jsh` script) with others.
- How do you load a previously saved set of snippets into a new session?Use `/open <file>`, which executes each snippet in the file; or pass the file on the command line: `jshell file.jsh`.
- What's the difference between `/drop` and `/reset`?`/drop` removes one named/id'd snippet; `/reset` clears the entire session back to empty.
saying these in an interview costs you the question
- Thinking `/list` etc. are Java syntax rather than tool commands
- Believing scripts must be `.java` files (they're snippet files, e.g. `.jsh`)
- Confusing `/reset` (clears session) with `/drop` (removes one snippet)
- Assuming `/save` produces a compilable class—it saves raw snippets