Why does IntelliJ give me Package doesn't exist error?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
IntelliJ's "Package doesn't exist" error means the compiler cannot locate a package referenced in an import statement. The most common causes are: the dependency is missing from your build file (pom.xml or build.gradle), the source directory is not marked as a Sources Root, or IntelliJ's internal caches are out of sync. Each of these has a specific fix, and this article walks through all of them systematically.
How IntelliJ Resolves Packages
IntelliJ resolves packages by consulting 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 cannot be found in any of these sources, IntelliJ reports "Package doesn't exist" during compilation. The error can also appear as red underlines in the editor before you even compile.
Cause 1: Missing Dependency
The most common cause. The package belongs to an external library that has not been added to the project's 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
Verifying the dependency is resolved
If the dependency does not appear in the tree, the build file has a syntax error or the dependency coordinates are wrong.
Cause 2: Source Directory Not Marked as Sources Root
IntelliJ needs to know which directories contain source code. If a directory is not marked correctly, all packages under it become invisible.
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
Fix
- Right-click the directory (typically
src/main/java) - Select Mark Directory as > Sources Root
For test directories, mark them as Test Sources Root (src/test/java).
In a standard Maven or Gradle project, IntelliJ should detect these automatically. If it does not, the project import may have failed. Re-import the project:
- File > Invalidate Caches... > Invalidate and Restart (nuclear option)
- Or delete the
.ideadirectory and re-open the project
Project structure for reference
Cause 3: IntelliJ Caches Out of Sync
IntelliJ maintains internal indexes and caches for fast code navigation. These can occasionally fall out of sync with the actual project state, especially after branch switches, merges, or external build tool changes.
Quick fix: Invalidate caches
File > Invalidate Caches... > Invalidate and Restart
This clears all internal caches and forces IntelliJ to re-index the entire project. It takes a few minutes on large projects but resolves most phantom errors.
Reimport the build model
For Maven:
Right-click pom.xml > Maven > Reimport
For Gradle:
Open the Gradle tool window > click the refresh (sync) icon
Delete .idea and .iml files
As a last resort, close the project and delete the .idea directory and any .iml files. Then reopen the project. IntelliJ regenerates these from the build files.
Cause 4: Wrong JDK Version
If the project uses APIs from a newer JDK than what IntelliJ is configured to use, packages like java.net.http (Java 11+) or java.util.stream.Gatherers (Java 22+) will appear missing.
Check the project SDK
File > Project Structure > Project > SDK
Ensure the JDK version matches what the project expects. Check the build file:
Check module language level
File > Project Structure > Modules > Sources > Language Level
Each module can have its own language level. If this is set to a version lower than the source code requires, certain packages and language features will not resolve.
Cause 5: Multi-Module Project Misconfiguration
In multi-module Maven or Gradle projects, IntelliJ sometimes fails to recognize inter-module dependencies.
Symptoms
- Module A uses classes from Module B
- IntelliJ shows "Package doesn't exist" for Module B's packages
- Command-line build works fine
Fix for Maven
Ensure Module A declares a dependency on Module B:
Then reimport the Maven project.
Fix for Gradle
Then sync the Gradle project.
Cause 6: Typo in Import Statement
Sometimes the issue is simply a wrong package name:
Use IntelliJ's auto-import feature (Alt+Enter on the unresolved symbol) to get the correct import. This avoids typos entirely.
Systematic 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 |
If the command-line build succeeds but IntelliJ fails, the problem is always in IntelliJ's configuration, not in the code.
Common Pitfalls
- Adding a dependency to the wrong module. In multi-module projects, each module has its own build file. The dependency must be declared in the module that uses it, not just in the root or parent project.
- Not reloading the Maven/Gradle project after editing the build file. IntelliJ does not automatically pick up changes to
pom.xmlorbuild.gradle. You must trigger a reimport manually or enable auto-import in settings. - Confusing the JDK version with the language level. The project SDK might be JDK 21, but the module language level could be set to 11. In that case, APIs introduced after JDK 11 will not resolve.
- Leaving stale
.ideaor.imlfiles in version control. If these files reference modules or JDKs that do not exist on another developer's machine, the project opens with errors. Either commit them consistently or.gitignorethem. - Not checking the terminal build first. If
mvn compileor./gradlew buildfails, the problem is in the project configuration itself, not in IntelliJ. Fix the build first, then reimport. - Running Invalidate Caches as the first step. This is a slow operation that should be a last resort. Try reimporting the build model first, which is faster and resolves most issues.
Summary
- The "Package doesn't exist" error in IntelliJ means the compiler cannot locate a package referenced in an import statement.
- The most common cause is a missing dependency in
pom.xmlorbuild.gradle. Add the dependency and reimport the project. - If source directories are not marked as Sources Root, IntelliJ cannot see the packages they contain. Right-click the directory and mark it correctly.
- Stale IntelliJ caches can produce phantom errors. Reimport the build model first; invalidate caches as a last resort.
- Verify the project SDK and module language level match the JDK version the code requires.
- Always test with a command-line build (
mvn compileor./gradlew build) to determine whether the problem is IntelliJ-specific or a genuine build issue.

