IntelliJ
Coding Issues
Software Errors
Package Management
Java Programming

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:

  1. Source roots. Directories marked as "Sources Root" in the project structure. Java files under these directories form the project's own packages.
  2. Dependencies. Libraries declared in Maven (pom.xml), Gradle (build.gradle), or added manually to the project's module dependencies.
  3. 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

xml
1<!-- pom.xml -->
2<dependencies>
3    <dependency>
4        <groupId>com.google.guava</groupId>
5        <artifactId>guava</artifactId>
6        <version>33.0.0-jre</version>
7    </dependency>
8</dependencies>

After adding the dependency, reload the Maven project:

  • Press Ctrl+Shift+O (Windows/Linux) or Cmd+Shift+I (macOS) when IntelliJ shows the "Load Maven Changes" notification
  • Or right-click the pom.xml and select Maven > Reload Project

Gradle

groovy
1// build.gradle
2dependencies {
3    implementation 'com.google.guava:guava:33.0.0-jre'
4}

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

bash
1# Maven
2mvn dependency:tree | grep guava
3
4# Gradle
5./gradlew dependencies --configuration runtimeClasspath | grep guava

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 compile or ./gradlew build succeeds)
  • 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:

  1. Right-click the directory (typically src/main/java)
  2. 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:

 
1my-project/
2  src/
3    main/
4      java/           <-- Sources Root (blue folder)
5        com/
6          example/
7            MyClass.java
8      resources/       <-- Resources Root
9    test/
10      java/           <-- Test Sources Root (green folder)
11  pom.xml

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:

  1. 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.
  2. 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.
  3. Delete .idea and .iml files. As a last resort, close the project, delete them, and reopen. IntelliJ regenerates them from the build files.
bash
# From the project root
rm -rf .idea
find . -name "*.iml" -delete

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:

xml
1<!-- Maven -->
2<properties>
3    <maven.compiler.source>21</maven.compiler.source>
4    <maven.compiler.target>21</maven.compiler.target>
5</properties>
groovy
1// Gradle
2java {
3    sourceCompatibility = JavaVersion.VERSION_21
4    targetCompatibility = JavaVersion.VERSION_21
5}

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:

xml
1<!-- Module A's pom.xml -->
2<dependencies>
3    <dependency>
4        <groupId>com.example</groupId>
5        <artifactId>module-b</artifactId>
6        <version>${project.version}</version>
7    </dependency>
8</dependencies>

For Gradle, add the project dependency, then sync:

groovy
1// Module A's build.gradle
2dependencies {
3    implementation project(':module-b')
4}

Cause 6: a typo in the import

java
1// Wrong: lowercase 'a' in ArrayList's package
2import java.util.arraylist;  // Package doesn't exist
3
4// Correct
5import java.util.ArrayList;

Use Alt+Enter on the unresolved symbol to let IntelliJ add the correct import and avoid typos entirely.

Debugging checklist

StepActionWhat It Fixes
1Verify the import statement is spelled correctlyTypos
2Check pom.xml or build.gradle for the dependencyMissing library
3Run mvn compile or ./gradlew build from the terminalConfirms whether the issue is IntelliJ-specific
4Reimport/refresh the Maven or Gradle projectStale build model
5Verify source directories are marked correctlyWrong Sources Root
6Check Project SDK and module language levelJDK version mismatch
7Invalidate Caches and RestartCorrupt IntelliJ indexes
8Delete .idea and .iml files, reopen projectDeeply 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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms