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:
- 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.
- 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.
| Cause | What Happened | Typical Symptom |
| Build file change not synced | You added a dependency to pom.xml or build.gradle but IntelliJ did not reimport | New classes show as unresolved |
| Corrupted IDE cache | Index files are stale or corrupted | Random symbols unresolved across the project |
| Wrong JDK configured | IntelliJ points to JDK 11 but the project uses JDK 17 | java.lang.Record or sealed classes unresolved |
| Module not marked as source | A directory is not recognized as a source root | All classes in that directory are unresolved |
| Generated sources missing | Annotation processors or code generators have not run | Generated classes (e.g., Lombok, MapStruct) unresolved |
| Multi-module dependency issue | Module A depends on Module B but IntelliJ does not see the dependency | Classes 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:
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.
- Open File > Project Structure > Project.
- Set SDK to the correct JDK.
- Set Language Level to match (e.g., "17" for JDK 17 features).
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.
- Open File > Project Structure > Modules, select the module and open the Sources tab.
src/main/javashould be marked Sources (blue folder).src/test/javashould be marked Tests (green folder).- 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:
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.
- Go to File > Invalidate Caches.
- Check all options.
- Click 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:
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.
| Step | Action | Time |
| 1 | Reimport Maven/Gradle project | 10-30 seconds |
| 2 | Check Project SDK and Language Level | 30 seconds |
| 3 | Verify source roots are marked correctly | 1 minute |
| 4 | Run mvn compile or ./gradlew compileJava for generated sources | 30 seconds |
| 5 | Invalidate Caches and Restart | 2-5 minutes |
| 6 | Delete .idea and reimport from scratch | 5-10 minutes |
Start at the top and stop at the step that fixes the issue.
Related reading
- Intellij maven project Fatal error compiling invalid flag --release
- IntelliJ Never use wildcard imports
- IntelliJ Process finished with exit code 0 when spring-boot run
- intellij run configuration can't find spring boot class
- IntelliJ shows method parameter hints on usage - How to disable it
- Intermediate layer makes tensorflow optimizer to stop working
- IntelliJ show JavaDocs tooltip on mouse over
- Intellij, target JRE vesion doesn''t match project jdk version

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.