Sub topic 6
Default imports and adding your own
This chapter assumes you can start JShell and enter snippets. It also assumes you know what a Java import statement does. Here you will see which packages JShell imports for you, how to add more, and how to fix the "cannot find symbol" errors that appear when an import is missing.
What JShell imports for you
A compiled Java file sees only java.lang automatically. JShell also imports ten packages at startup, so common classes work without any import lines:
java.io.*java.math.*java.net.*java.nio.file.*java.util.*java.util.concurrent.*java.util.function.*java.util.prefs.*java.util.regex.*java.util.stream.*
That is why List, Map, IntStream, Path, BigDecimal and Pattern all work in a fresh session:
jshell> IntStream.rangeClosed(1, 5).sum()
$1 ==> 15
jshell> List<String> names = List.of("Ada", "Grace")
names ==> [Ada, Grace]The exact list can change between JDK versions. Check your own session instead of relying on memory.
Listing the active imports
The /imports command prints every import in effect, both the defaults and the ones you added:
jshell> /imports
| 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.*The defaults come from a built-in startup script. /list -start shows the snippets that script runs, each with an ID that begins with s, such as s1. Snippets you type get plain numeric IDs.
What happens when an import is missing
Packages outside the default list, such as java.time, are not visible. Using one of their classes gives a compile error. The session stays alive.
jshell> LocalDate.of(2026, 11, 26)
| Error:
| cannot find symbol
| symbol: variable LocalDate
| LocalDate.of(2026, 11, 26)
| ^-------^The message says variable because JShell first assumes an unknown name before a dot is a variable. The real problem is a missing import.
Adding your own imports
Type an ordinary import statement. The semicolon is optional in JShell, as it is for other snippets.
jshell> import java.time.*
jshell> LocalDate.of(2026, 11, 26).getDayOfWeek()
$2 ==> THURSDAYJShell accepts a single-type import, an on-demand import (.*) and a static import:
jshell> import java.time.LocalDate
jshell> import static java.lang.Math.*
jshell> sqrt(144)
$3 ==> 12.0
jshell> max(3, 9)
$4 ==> 9A static import lets you call sqrt without the Math. prefix. That suits quick calculations, though you may want Math.sqrt in code you plan to move into a real program.
The import stays active until the session ends, you remove it, or you run /reset. It also applies to snippets you enter later, including methods and classes. Imports do not apply retroactively to anything that already failed to compile. Re-enter that snippet after adding the import.
Getting help from tab completion
Tab completion can help you find the import. Type a class name that is not imported, then press Tab. JShell lists matching classes in the packages it can see. Press Shift+Tab and then i after a complete class name, and JShell offers a numbered list of imports to add. This is faster than remembering the package, and it adds the correct import for you.
How a name is resolved
JShell resolves a simple name by the same rules as compiled Java. The diagram shows the order.
This resolution order produces the behaviors in the next section.
Ambiguous names
Two on-demand imports can both provide the same simple name. The classic case is List, which exists in both java.util and java.awt. Add java.awt.* and the name becomes ambiguous:
jshell> import java.awt.*
jshell> List<String> items = new ArrayList<>()
| Error:
| reference to List is ambiguous
| both interface java.util.List in java.util and class java.awt.List in java.awt matchThere are two fixes. The first is a single-type import, which wins over any on-demand import:
jshell> import java.util.List
jshell> List<String> items = new ArrayList<>()
items ==> []The second is to write the fully qualified name, such as java.awt.List, only where you need that class. Prefer single-type imports when you add packages with common names, such as java.awt, java.sql (which has Date) or javax.swing.
Removing and resetting imports
Imports are snippets, so they have IDs. Run /list to see them, then remove one with /drop:
jshell> /list
1 : import java.time.*;
2 : import static java.lang.Math.*;
jshell> /drop 1
| dropped import java.time.*If a variable or method depends on a dropped import, JShell marks that snippet as needing a new declaration. It does not delete the snippet. Add the import again and it works again.
The /reset command is the strong option. It clears all variables, methods, classes and your added imports, then re-runs the startup script, so the ten defaults return. Use it when a session has drifted into an unclear state.
Making imports permanent
Imports you type live only for the current session. To have an import in every session, change the startup script. A command such as /set start does this, and a later chapter covers it. For one project, a simpler approach is to put the import lines at the top of a script file and load it, as in /open setup.jsh. This keeps the setup explicit and shareable.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `cannot find symbol` on a class such as `LocalDate` | Package not in the default imports | `import java.time.*` |
| `reference to X is ambiguous` | Two on-demand imports supply the same name | Add a single-type import |
| A method is "created, however, it cannot be invoked" | It uses a class that is not imported yet | Add the import, then call the method |
| Import gone after restarting `jshell` | Imports last only for the session | Use a script or the startup configuration |
Summary of the working model
- JShell imports ten packages by default, and
/importsshows the full list. - You add imports by typing normal
importstatements. Static imports work. - Imports are snippets. List them with
/list, remove them with/drop, and restore the defaults with/reset. - Name resolution follows normal Java rules, so a single-type import beats an on-demand one.
Exercise
In a fresh JShell session you run Duration.ofMinutes(90) and get "cannot find symbol". What do you type to fix it, and how do you confirm the fix took effect?
Show answer
Duration is in java.time, which is not a default import. Type import java.time.Duration (or import java.time.*), then run Duration.ofMinutes(90) again, which gives PT1H30M. Run /imports to confirm the new import appears in the list.
Exercise
You imported java.awt.*, and now List<String> xs = new ArrayList<>() fails with an ambiguity error. Give the smallest fix and explain why it works.
Show answer
Add import java.util.List. A single-type import takes precedence over on-demand imports such as java.util.* and java.awt.*, so the simple name List now refers only to java.util.List.
Exercise
What is the difference between /drop on an import and /reset?
Show answer
/drop removes one snippet, here a single import, and leaves everything else in place. /reset clears the whole session state, including variables, methods, classes and your added imports. It then re-runs the startup script, so only the default imports remain.