created on | January 28, 2022 |
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.
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
where is the script file and options is one of:
Script files can be loaded with the command. The general form is
jshell comes with three predefined script files:
The script file DEFAULT loads the shell default imports, which includes the following packages:
This script is loaded at jshell startup. This can be overwritten with
The script files JAVASE imports all packages available in Java SE
Tired of typing System.out.println(whatever)? Load the predefined script and you use the methods , and of the class PrintStream.
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.:
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:
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:
Or, for the example above, define and persist the startup configuration in one go: