Sub topic 1
Starting and exiting JShell
JShell is the read-eval-print loop (REPL) that ships with the JDK. It reads one piece of Java input, evaluates it, prints the result, and waits for more. This chapter covers how to launch it, how to read what it prints, and how to leave it without surprises.
Prerequisites
This chapter assumes you have the following:
- A JDK, version 9 or later, installed. A JRE alone does not include
jshell. - The
jshelltool on yourPATH. It lives in thebindirectory of the JDK, next tojavaandjavac. - Basic Java syntax: variables, types, methods and classes.
- Comfort using a terminal.
To check the first two points, run this in your terminal:
jshell --versionIt prints a line such as jshell 21.0.2. The number depends on your JDK. If you get "command not found", the JDK's bin directory is not on your PATH. See the troubleshooting section below.
Starting a session
Type jshell at a shell prompt and press Enter:
jshellJShell prints a banner and then its own prompt, jshell>:
$ jshell
| Welcome to JShell -- Version 21
| For an introduction type: /help intro
jshell> 6 * 7
$1 ==> 42
jshell> /exit
| GoodbyeThe version number in the banner matches your JDK. Everything after jshell> is what you typed. Lines that start with | are messages from JShell itself, not output of your code.
JShell keeps running until you ask it to stop. It holds all variables, methods and classes you define in memory for the lifetime of the process.
Reading the output
JShell reports each result in one of a few shapes. The default mode, called normal feedback, uses these:
| You typed | JShell prints | Meaning | Column |
|---|---|---|---|
| `6 * 7` | `$1 ==> 42` | An expression with no variable gets a scratch variable. | |
| `int n = 5` | `n ==> 5` | A named variable was created. | |
| `int sq(int x) { return x * x; }` | `\ | created method sq(int)` | A method was declared. |
| `System.out.println("hi")` | `hi` | A statement with no value prints only its own output. |
A statement such as System.out.println("hi") produces no ==> line because it is a void call and has no value to report.
If you press Enter in the middle of an unfinished construct, such as an open brace, JShell shows a continuation prompt and waits for the rest:
jshell> int add(int a, int b) {
...> return a + b;
...> }
| created method add(int, int)The continuation prompt is ...>. It means JShell has not yet seen a complete snippet.
Getting help inside the session
Two commands are worth knowing before you do anything else:
/helplists all slash commands./help introprints a short introduction to the tool.
Slash commands control the session. They are not Java code, so they always begin with /. Later chapters cover them in detail.
Startup options
You can pass options when you launch. These are the ones you will use most:
| Option | Effect |
|---|---|
| `--version` | Print the version and exit. |
| `-v` | Start in verbose feedback mode, which explains more about each result. |
| `-s` | Start in silent feedback mode, which prints almost nothing. |
| `-q` | Start in concise feedback mode. |
| `--class-path path` | Make classes from the given jar or directory available. |
| `--help` | Print the full list of options. |
For example, to see the extra messages JShell can give you:
jshell -vFeedback modes are covered in their own chapter. For now, remember that the options are processed at launch and apply to that session only.
Leaving a session
There are three ways out. Only one of them is a command.
- Type
/exitand press Enter. JShell prints| Goodbyeand returns you to the shell. - Press Ctrl+D at an empty
jshell>prompt. This sends end-of-input and ends the session the same way. - Close the terminal window. This also ends it, but without the goodbye message.
Do not type exit without the slash. JShell reads it as a Java expression, and since no variable named exit exists, it reports an error and stays open.
jshell> exit
| Error:
| cannot find symbol
| symbol: variable exit
| exit
| ^--^
jshell> /exit
| GoodbyeCtrl+C is not an exit key
Ctrl+C does not end the session. At the prompt, it discards the line you are typing. While your code is running, it interrupts that code and returns you to the prompt. This matters if you start an infinite loop by accident:
jshell> while (true) { }Press Ctrl+C and the loop stops. Your variables and methods are still there.
Exit status
In recent JDKs (12 and later), /exit accepts an integer expression. JShell uses it as the process exit status, which is useful in scripts:
jshell> /exit 3
| GoodbyeBack in the shell, echo $? prints 3 on Linux and macOS. On older JDKs the status is always 0.
What you lose when you exit
Everything in the session lives in memory. When you exit, the variables, methods, classes and scratch variables are gone. Starting jshell again gives you a fresh session in which $1 does not exist.
If you want to keep your work, save it first. The /save command writes your snippets to a file, and a later chapter on scripts shows how to reload them. You can also clear state without leaving by running /reset, which keeps the process open but discards all snippets.
Troubleshooting a launch that fails
These are the common problems when starting JShell:
jshell: command not found. Either the JDK'sbindirectory is not onPATH, or the JDK is older than 9. Runjava -versionto check the version. If it is 9 or later, add$JAVA_HOME/bintoPATH.- JRE installed but no
jshell. Some distributions ship a runtime without development tools. Install a full JDK. - Java 8 in
PATHfirst. A newer JDK may be installed but shadowed by an older one.which jshell(orwhere jshellon Windows) shows which copy runs. - Slow first start. JShell starts a separate execution engine process behind the scenes. A delay of a second or two on the first launch is normal.
The last point explains one behavior you might notice later. Your code does not run inside the jshell tool's own process. It runs in a remote agent JVM that JShell launches and controls. This is why Ctrl+C can interrupt your code without killing the session, and why options for the agent JVM use the -R prefix, for example -R-Xmx512m.
A short workflow
The diagram shows the life of a session from launch to exit.
Exercises
Exercise
You type exit at the jshell> prompt and see "cannot find symbol". What did you do wrong, and what is the correct input?
Show answer
exit without a slash is read as a Java expression, and no variable with that name exists. Slash commands start with /, so the correct input is /exit. Ctrl+D at an empty prompt also works.
Exercise
A loop you started with while (true) { } has frozen your prompt. You want to stop it but keep every variable you have defined. What do you press, and why is it better than closing the terminal?
Show answer
Press Ctrl+C. It interrupts the running code and returns you to the jshell> prompt with the session state intact. Closing the terminal ends the process and discards all variables, methods and classes.
Exercise
You run jshell and the shell says "command not found". Name two checks that identify the cause.
Show answer
Run java -version to confirm the JDK is version 9 or later. Check whether the JDK's bin directory (for example $JAVA_HOME/bin) is on PATH, using which jshell or where jshell. A JRE-only install or an older JDK first on PATH are the usual causes.