JShell guide - script files

created onJanuary 28, 2022

working with jshell script files

A jshell script file is a sequence of jshell commands and/or snippets, one command or snippet per line. jshell script files can be created or edited with an external editor or from the current jshell session with the /save command.

saving commands and snippets to a script file

Commands and snippets that have been entered in the current session can be saved to jshell script file. The general form of the command to save commands and snippets to a file is

/save [options] <file>

where is the script file and options is one of:

  • includes all snippets, including failed, overwritten and dropped snippets.
  • includes the snippets that have been loaded at the startup of the current jshell session.
  • includes all command and snippets that have been entered in the current session, including failed commands and failed, overwritten and dropped snippets.
  • the ids or ids of the snippets to save. Several ids can be provided, separated by space. Id ranges can also be provided. Failed, overwritten and dropped snippets are not saved here.

loading sript files

Script files can be loaded with the command. The general form is

/open <file>

predefined script files

jshell comes with three predefined script files:

  • DEFAULT
  • JAVASE
  • PRINTING

DEFAULT

The script file DEFAULT loads the shell default imports, which includes the following packages:

  • java.io
  • java.math
  • java.net
  • java.nio.file
  • java.util
  • import java.util.concurrent
  • import java.util.function
  • import java.util.prefs
  • import java.util.regex
  • import java.util.stream

This script is loaded at jshell startup. This can be overwritten with

/set start <file>

JAVASE

The script files JAVASE imports all packages available in Java SE

PRINTING

Tired of typing System.out.println(whatever)? Load the predefined script and you use the methods , and of the class PrintStream.

configuring jshell with startup script files

jshell can be configured by defining script files that are loaded on startup of jshell. The default is the predefined script file DEFAULT.

Startup script files are set with the command, i.e.:

jshell> /set start DEFAULT PRINTING jshell>

Several startup script files can be supplied, separated by space. You can also supply some custom script files, i.e. for setting the classpath for importing packages you often use in jshell.

If the new startup configuration should be active in the current session, the current session must be reset with the command:

jshell>jshell> /reset | Resetting state. jshell>

If you want the startup configuration to be persistent, so that the configuration is saved and used from now on in all future jshell sessions, you can use the command with the parameter . You can either persist the current startup configuration with:

jshell> /set start -retain jshell>

Or, for the example above, define and persist the startup configuration in one go:

jshell> /set start -retain DEFAULT PRINTING jshell>