Sub topic 14
Customizing the startup script
This chapter assumes you can start JShell, declare snippets, run slash commands such as /list and /vars, and load a script file. It uses JDK 11 or later. The startup options shown here exist in JDK 9, but a few details differ there.
What the startup script is
Every time JShell starts, and every time you run /reset or /reload, it feeds a set of snippets to the session before you type anything. This set is the startup configuration. By default it is a block of import statements. That is why List, Map and IntStream work without imports.
You can replace or extend this configuration. Typical reasons:
- Import packages you use every day, such as
java.time. - Define small helper methods you want in every session.
- Add shorthand output methods, such as
println. - Start with an empty session for a clean experiment.
The diagram shows when the configuration is applied.
Predefined startup entries
JShell ships with named startup entries. You can use them by name wherever a file is accepted.
| Name | What it contains |
|---|---|
| `DEFAULT` | Imports for `java.io`, `java.math`, `java.net`, `java.nio.file`, `java.util`, `java.util.concurrent`, `java.util.function`, `java.util.prefs`, `java.util.regex` and `java.util.stream` |
| `PRINTING` | Shorthand methods `print`, `println` and `printf` that forward to `System.out` |
| `JAVASE` | Imports for all Java SE packages, which is a long list |
Most sessions use only DEFAULT. Use /list -start to see the startup snippets that are active now. They carry ids starting with s, such as s1. A plain /list hides them.
Setting the startup configuration
The /set start command changes the configuration. It takes one or more entries, which can be predefined names or file paths, and loads them in the order given.
jshell> /set start DEFAULT PRINTING
jshell> /reset
| Resetting state.
jshell> println("no System.out needed")
no System.out needed/set start alone does not change the session you are in. The new configuration takes effect at the next /reset, /reload or launch. It also applies only to this JShell process until you retain it.
To make it permanent, add -retain:
jshell> /set start -retain DEFAULT PRINTINGJShell stores a retained setting in the Java preferences store, not in a file you can edit. The location depends on your operating system. Retained settings apply to every future session, so choose them carefully.
Other forms of the command:
/set start -defaultreturns toDEFAULT./set start -nonestarts empty. No imports are loaded./set start -retainwith no entries keeps the current setting.
Writing your own startup file
A startup file is an ordinary text file of snippets. It follows the same rules as any script. Here is one that adds time helpers.
import java.time.*;
import java.time.format.DateTimeFormatter;
String now() {
return LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
void p(Object o) {
System.out.println(o);
}Save it as ~/jshell-startup.jsh. Because a custom entry does not include the defaults, list DEFAULT first if you still want them:
jshell> /set start -retain DEFAULT /home/me/jshell-startup.jsh
jshell> /reset
| Resetting state.
jshell> p(now())
2026-11-26T06:17:11.530The timestamp you see will differ. Use an absolute path. A relative path is read from the directory where you run the command, which is easy to get wrong later.
Entry order matters. JShell runs entries in the order given, so a file that uses LocalDateTime needs its own import or an earlier entry that provides it. In the file above, the import line sits first for this reason.
One-off configuration at launch
You can override the startup configuration for one launch with command-line options. The retained setting stays unchanged.
jshell --startup DEFAULT --startup PRINTING
jshell --no-startup--startup can be repeated and takes the same entries as /set start. --no-startup gives an empty session. This is useful for reproducing a bug report without your personal helpers.
Do not confuse these options with the load files you can give at the end of the command line:
jshell DEFAULT PRINTING my-session.jshLoad files run after the startup configuration finishes. They run as if you had typed them. Unlike startup entries, they are listed by a plain /list, and /reset does not replay them. Use a load file for work specific to one task. Use the startup configuration for things you want every time.
How startup snippets differ from normal snippets
Startup snippets behave almost like typed snippets, with these exceptions:
- They are hidden from a plain
/list. Use/list -startfor the startup snippets or/list -allfor everything. /saveskips them unless you pass-startor-all. To copy your startup snippets into a file, run/save -start startup-copy.jsh./resetbrings them back. If you redefine a startup method during a session,/resetreturns it to the startup definition.- Their ids use the
sprefix, so they never collide with the numbered ids of your own snippets.
Failure modes
A startup entry fails. If a snippet in a startup file does not compile, JShell reports the error when it applies the startup configuration. Later snippets still run, but anything depending on the failed one will be unresolved. A common cause is a misspelled package name in an import. Fix the file, then run /reset.
A bad retained setting. If a retained file is moved or deleted, JShell cannot load it on the next launch. Start with jshell --no-startup, then run /set start -retain -default to restore the defaults.
Slow startup. JAVASE imports the whole platform, and large helper files add time at each launch. Keep startup files small. Put heavy setup in a load file instead.
Hidden dependence. A snippet you paste into a compiled program will not compile there if it relied on a startup import or helper. Compiled Java needs its own imports. Before you share code, test it with jshell --no-startup, or add the imports explicitly.
Summary of commands
| Task | Command |
|---|---|
| See active startup snippets | `/list -start` |
| Use defaults plus printing helpers this session | `/set start DEFAULT PRINTING`, then `/reset` |
| Keep a configuration for all future sessions | `/set start -retain ...` |
| Return to the defaults | `/set start -retain -default` |
| Start empty for one launch | `jshell --no-startup` |
| Export startup snippets | `/save -start file.jsh` |
Exercise
You retained a startup file that defines int square(int x). During a session you redefine square to return x * x * 1. What happens to square after you run /reset, and how would you launch a session that has no personal helpers at all?
Show answer
/reset replays the startup snippets, so square returns to the definition in the startup file, and your redefinition is lost. Launch with jshell --no-startup to get a session with no imports and no helpers. This applies to that launch only and does not change the retained setting.
Exercise
You want java.time.* imported in every session, plus the default imports. Which command do you use, assuming the file /home/me/time.jsh contains import java.time.*;, and why must DEFAULT appear in the command?
Show answer
Run /set start -retain DEFAULT /home/me/time.jsh. A custom entry replaces the current configuration instead of adding to it, so without DEFAULT the standard imports such as java.util.* would be missing. The change takes effect on the next launch or /reset.