IntelliJ IDEA
Code Compilation
Software Debugging
Programming Issues
Java Development

IntelliJ "Cannot resolve symbol" but the code still compiles: how to fix it

Codemia Knowledge Hub ·

When IntelliJ shows "Cannot resolve symbol" but the code compiles and runs, IntelliJ's internal project model has fallen out of sync with your real build configuration. The build tool (Maven, Gradle or javac) resolves its own classpath correctly, so the code is fine; the IDE's index is wrong. The fix is almost always one of three things: reimport the build project, invalidate IntelliJ's caches, or correct the SDK and module configuration.

Why the IDE and the build disagree

IntelliJ runs two separate systems:

  1. The IDE's indexer and inspections, which power highlighting, completion, navigation and error checks. They build a model of the project from source files and dependency metadata.
  2. The external build tool (Maven, Gradle or javac), which does the actual compilation with its own classpath, dependency resolution and compiler settings.

When they disagree about which classes and methods exist, you get the inspection error even though compilation succeeds.

CauseWhat HappenedTypical Symptom
Build file change not syncedYou added a dependency to pom.xml or build.gradle but IntelliJ did not reimportNew classes show as unresolved
Corrupted IDE cacheIndex files are stale or corruptedRandom symbols unresolved across the project
Wrong JDK configuredIntelliJ points to JDK 11 but the project uses JDK 17java.lang.Record or sealed classes unresolved
Module not marked as sourceA directory is not recognized as a source rootAll classes in that directory are unresolved
Generated sources missingAnnotation processors or code generators have not runGenerated classes (e.g., Lombok, MapStruct) unresolved
Multi-module dependency issueModule A depends on Module B but IntelliJ does not see the dependencyClasses from Module B unresolved in Module A

Fix 1: Reimport the Maven or Gradle project

Do this first; it fixes most cases in seconds.

Maven: open the Maven tool window (View > Tool Windows > Maven) and click Reload All Maven Projects (the circular arrows). Or press Ctrl+Shift+A (Cmd+Shift+A on macOS), type "Reload All Maven Projects" and press Enter.

Gradle: open the Gradle tool window and click Reload All Gradle Projects.

IntelliJ re-reads the build files, re-resolves dependencies and updates its project model. Note that Build > Build Project does not do this: building does not update IntelliJ's dependency model, so it will not fix a missing dependency.

If the IDE is stuck, refresh from the command line and then reimport:

bash
1# Maven: force dependency re-download and update
2mvn dependency:purge-local-repository
3mvn clean install
4
5# Gradle: refresh dependencies
6./gradlew --refresh-dependencies build

Fix 2: Check the project SDK and language level

If newer APIs such as records or sealed classes are unresolved, the configured SDK probably does not match the JDK features you use. This is common when several JDKs (say 8, 11 and 17) are installed and IntelliJ picked a different one from your build tool.

  1. Open File > Project Structure > Project.
  2. Set SDK to the correct JDK.
  3. Set Language Level to match (e.g., "17" for JDK 17 features).
 
File > Project Structure > Project
  SDK: corretto-17 (or your JDK)
  Language Level: 17

Then open File > Project Structure > Modules and make sure no module has a lower Language Level than the project default.

Fix 3: Mark source roots correctly

IntelliJ only indexes directories marked as source roots.

  1. Open File > Project Structure > Modules, select the module and open the Sources tab.
  2. src/main/java should be marked Sources (blue folder).
  3. src/test/java should be marked Tests (green folder).
  4. If annotation processors write to target/generated-sources/annotations, mark it Generated Sources Root.

A Maven reimport usually sets these correctly; manually configured projects may need them set by hand.

Fix 4: Generate sources for Lombok, MapStruct and Dagger

If the unresolved symbols come from code generators, the generated sources may not exist yet. Run the build:

bash
1# Maven: run annotation processing
2mvn compile
3
4# Gradle: run annotation processing
5./gradlew compileJava

Then go to Settings > Build, Execution, Deployment > Compiler > Annotation Processors, check Enable annotation processing, and reimport.

For Lombok, also install the Lombok IntelliJ plugin (Settings > Plugins > search "Lombok"). The dependency in pom.xml is enough for compilation, but IntelliJ needs the plugin to understand @Getter, @Setter and the other annotations; without it every Lombok-generated method shows as unresolved.

Fix 5: Invalidate caches and restart

If reimporting does not help, the index itself may be corrupted.

  1. Go to File > Invalidate Caches.
  2. Check all options.
  3. Click Invalidate and Restart.
 
File > Invalidate Caches > Check all boxes > Invalidate and Restart

IntelliJ rebuilds its whole index, which takes a few minutes on large projects. Because of that cost, try a reimport before reaching for this.

Fix 6: Delete .idea and reimport

As a last resort, remove IntelliJ's project configuration entirely:

bash
1# Close IntelliJ first
2rm -rf .idea
3rm -f *.iml
4rm -f **/*.iml

Reopen the project by opening pom.xml or build.gradle as a project, and IntelliJ creates a fresh .idea directory. This loses custom run configurations, code style overrides and inspection profiles stored in .idea; if your team commits .idea files, restore them from Git afterwards.

Wait for indexing before judging the result

After any reimport or cache invalidation, wait for the indexing progress bar at the bottom of the IDE to finish. Checking for errors before indexing completes gives false results.

And trust the build tool over the red squiggles: if mvn clean install succeeds, the code is correct and the IDE configuration is what needs fixing.

StepActionTime
1Reimport Maven/Gradle project10-30 seconds
2Check Project SDK and Language Level30 seconds
3Verify source roots are marked correctly1 minute
4Run mvn compile or ./gradlew compileJava for generated sources30 seconds
5Invalidate Caches and Restart2-5 minutes
6Delete .idea and reimport from scratch5-10 minutes

Start at the top and stop at the step that fixes the issue.


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