On startup, jshell imports a bunch of Java packages by default. Imported packages can be listed
with or . The output of the
command below lists the packages that are
imported in jshell by default on startup:
In the example above, I could have saved myself some typing by letting jshell import the
necessary package.
After typing just ‘Instant’:
jshell> Instant
… then pressing
(that is, press shift, hold it while pressing tab, release
both shift and tab and press i), two options are displayed:
jshell> Instant
0: Do nothing
1: import: java.time.Instant
Choice:
Choosing the second option by pressing
results in the class
java.time.Instant to be
imported. The original input is displayed again at the jshell prompt:
Semi-autoimport also works in combination with the create variable shortcut. Given that
OffsetDateTime has been imported beforehand, an OffsetDateTime
instance named ‘now’ can be
used to create a variable of type Instant by typing now.toInstant(),
then pressing
. jshell will provide three options, the third option is importing the missing
type Instant and creating a variable. Pressing
(for using the third option)
results in
the type Instant being imported, the variable type Instant and an equals
sign being printed
before the Instant.now() statement and the cursor being placed between variable type and
equals sign:
jshell> OffsetDateTime now = OffsetDateTime.now()
now ==> 2021-12-19T20:22:33.944732Z
jshell> now.toInstant()
0: Do nothing
1: Create variable
2: import: java.time.Instant. Create variable
Choice:
Imported: java.time.Instant
jshell> Instant | = now.toInstant()
The piping symbol ‘|’ denotes the position where shell places the cursor, the symbol is
not printed out by jshell.
importing all Java SE packages
All Java SE packages can be imported with
/open JAVASE
reset imports to default imports
With the imports can be reset to the default imports loaded
on startup of the jshell. Note the missing java.time package in the output
of the second command: