IntelliJ "package does not exist" error: causes and fixes
Codemia Knowledge Hub ·
IntelliJ's "Package doesn't exist" error means the compiler cannot find a package named in an import statement. The usual causes are a dependency missing from pom.xml or build.gradle, a source directory that is not marked as a Sources Root, or IntelliJ caches that are out of sync with the project. A wrong JDK, a multi-module misconfiguration or a typo in the import can also trigger it.
Where IntelliJ looks for packages
IntelliJ resolves packages from three sources:
- Source roots. Directories marked as "Sources Root" in the project structure. Java files under these directories form the project's own packages.
- Dependencies. Libraries declared in Maven (
pom.xml), Gradle (build.gradle), or added manually to the project's module dependencies. - JDK modules. Standard library packages provided by the configured JDK (e.g.,
java.util,java.io).
If a package is in none of them, IntelliJ reports the error during compilation, and often shows red underlines in the editor before you compile.
Cause 1: the dependency is missing
This is the most common cause: the package belongs to an external library that is not in the build configuration.
Maven
After adding the dependency, reload the Maven project:
- Press
Ctrl+Shift+O(Windows/Linux) orCmd+Shift+I(macOS) when IntelliJ shows the "Load Maven Changes" notification - Or right-click the
pom.xmland select Maven > Reload Project
Gradle
Reload the Gradle project:
- Click the Gradle elephant icon in the notification bar
- Or open the Gradle tool window and click the refresh button
IntelliJ does not pick up build file edits on its own. Reload every time, or enable auto-import in settings. In multi-module projects, declare the dependency in the module that uses it, not only in the root or parent project.
Check that the dependency resolves
If the dependency is missing from the tree, the build file has a syntax error or the coordinates are wrong.
Cause 2: the source directory is not a Sources Root
IntelliJ must know which directories hold source code. If a directory is not marked, every package under it is invisible.
Typical symptoms:
- Code compiles fine from the command line (
mvn compileor./gradlew buildsucceeds) - IntelliJ shows red underlines and "Package doesn't exist" in the editor
- The directory icon in the Project view is gray instead of blue
To fix it:
- Right-click the directory (typically
src/main/java) - Select Mark Directory as > Sources Root
Mark test directories (src/test/java) as Test Sources Root. In a standard Maven or Gradle project IntelliJ should detect these automatically; if it did not, the project import probably failed and you should re-import, as described under Cause 3.
Expected layout:
Cause 3: IntelliJ caches are out of sync
IntelliJ keeps indexes and caches for fast navigation, and they can drift from the real project state, especially after branch switches, merges or changes made by external build tools. Work through these from fastest to slowest:
- Reimport the build model. For Maven, right-click
pom.xml> Maven > Reimport. For Gradle, open the Gradle tool window and click the refresh (sync) icon. This is fast and fixes most cases. - Invalidate caches. File > Invalidate Caches... > Invalidate and Restart clears the caches and re-indexes the whole project. It takes a few minutes on large projects, so use it after reimporting has failed, not first.
- Delete
.ideaand.imlfiles. As a last resort, close the project, delete them, and reopen. IntelliJ regenerates them from the build files.
If these files are in version control and reference modules or JDKs that do not exist on another developer's machine, the project opens with errors there. Either commit them consistently or add them to .gitignore.
Cause 4: the wrong JDK version
If the code uses APIs from a newer JDK than IntelliJ is configured with, packages like java.net.http (Java 11+) or java.util.stream.Gatherers (Java 22+) appear to be missing.
Check the project SDK under File > Project Structure > Project > SDK and compare it with the version the build file expects:
Then check File > Project Structure > Modules > Sources > Language Level. Each module has its own language level, and it is separate from the SDK: the SDK can be JDK 21 while a module's language level is 11, in which case APIs added after JDK 11 will not resolve.
Cause 5: modules in a multi-module project are not linked
In multi-module Maven or Gradle projects, IntelliJ sometimes misses dependencies between modules. The symptom: Module A uses classes from Module B, IntelliJ reports Module B's packages as missing, and the command-line build works.
For Maven, make sure Module A declares a dependency on Module B, then reimport:
For Gradle, add the project dependency, then sync:
Cause 6: a typo in the import
Use Alt+Enter on the unresolved symbol to let IntelliJ add the correct import and avoid typos entirely.
Debugging checklist
| Step | Action | What It Fixes |
| 1 | Verify the import statement is spelled correctly | Typos |
| 2 | Check pom.xml or build.gradle for the dependency | Missing library |
| 3 | Run mvn compile or ./gradlew build from the terminal | Confirms whether the issue is IntelliJ-specific |
| 4 | Reimport/refresh the Maven or Gradle project | Stale build model |
| 5 | Verify source directories are marked correctly | Wrong Sources Root |
| 6 | Check Project SDK and module language level | JDK version mismatch |
| 7 | Invalidate Caches and Restart | Corrupt IntelliJ indexes |
| 8 | Delete .idea and .iml files, reopen project | Deeply broken project config |
Step 3 splits the problem in two. If the command-line build fails, fix the project configuration first and then reimport. If it succeeds but IntelliJ still fails, the problem is in IntelliJ's configuration, not in the code.
Related reading
- Why does invokeLater execute in the main thread?
- Why does Iterable<T> not provide stream() and parallelStream() methods?
- Why does Java 6 ArrayssortObject change from mergesort to insertionsort for small arrays?
- Why does Java allow us to compile a class with a name different than the file name?
- Why does Java have transient fields?
- Why does Java switch on contiguous ints appear to run faster with added cases?
- Why does Java use hash 0x7FFFFFFF tab.length to decide the index of a key?
- Why does Java's Arrays.sort method use two different sorting algorithms for different types?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.