JShell guide - imports

created onJanuary 28, 2022

default imports

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:

jshell> /import | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* jshell>

importing packages

Packages are imported like the usual way to import packages in Java:

jshell> import java.time.* jshell> Instant.now() $2 ==> 2021-12-15T02:55:49.828694Z jshell>

let the jshell import packages for you

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:

jshell> Instant 0: Do nothing 1: import: java.time.Instant Choice: Imported: java.time.Instant jshell> Instant

… which now can be completed to Instant.now():

jshell> Instant 0: Do nothing 1: import: java.time.Instant Choice: Imported: java.time.Instant jshell> Instant.now() $2 ==> 2021-12-15T04:41:51.853234Z jshell>

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:

jshell> import java.time.* jshell> /import | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* | import java.time.* jshell> /open DEFAULT jshell> /import | import java.time.* | import java.io.* | import java.math.* | import java.net.* | import java.nio.file.* | import java.util.* | import java.util.concurrent.* | import java.util.function.* | import java.util.prefs.* | import java.util.regex.* | import java.util.stream.* jshell>