IntelliJ inspection gives Cannot resolve symbol but still compiles code
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When IntelliJ shows "Cannot resolve symbol" but your code compiles and runs fine, the issue is a mismatch between IntelliJ's internal project model and your actual build configuration. IntelliJ maintains its own index of classes, methods, and dependencies for real-time code assistance, and this index can fall out of sync with what Maven, Gradle, or your JDK actually provides. The code compiles because the build tool uses its own classpath resolution, which is correct. The fix is almost always one of three things: reimport your build project, invalidate IntelliJ's caches, or correct the SDK/module configuration.
Why This Happens
IntelliJ runs two separate systems in parallel:
- The IDE's internal compiler and indexer: This powers syntax highlighting, auto-completion, navigation, and error inspections. It builds a model of your project by parsing source files and reading dependency metadata.
- The external build tool (Maven, Gradle, or javac directly): This handles actual compilation using its own classpath, dependency resolution, and compiler settings.
When these two systems disagree about what classes and methods are available, you get the "Cannot resolve symbol" inspection error even though compilation succeeds.
Root Causes
| 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 Build Project
This is the most common fix and should be your first action.
For Maven:
- Open the Maven tool window (View > Tool Windows > Maven).
- Click the Reload All Maven Projects button (circular arrows icon at the top).
Or use the keyboard: press Ctrl+Shift+A (or Cmd+Shift+A on macOS), type "Reload All Maven Projects," and press Enter.
For Gradle:
- Open the Gradle tool window.
- Click the Reload All Gradle Projects button.
This forces IntelliJ to re-read your build files, re-resolve dependencies, and update its internal project model.
From the command line (useful when the IDE is stuck):
After running these, reimport the project in IntelliJ.
Fix 2: Invalidate Caches and Restart
If reimporting does not help, the IDE's index may be corrupted.
- Go to File > Invalidate Caches.
- Check all options in the dialog.
- Click Invalidate and Restart.
IntelliJ will restart and rebuild its entire index from scratch. This takes a few minutes on large projects but resolves most index corruption issues.
After restart, wait for the indexing progress bar at the bottom of the IDE to complete before checking whether the errors are gone.
Fix 3: Check the Project SDK and Language Level
A mismatch between the configured SDK and the actual JDK features you are using causes resolution failures for newer APIs.
- Go to File > Project Structure > Project.
- Verify the SDK is set to the correct JDK version.
- Verify the Language Level matches (e.g., "17" for JDK 17 features).
Also check each module individually:
- Go to File > Project Structure > Modules.
- Select each module and verify its Language Level is not set to a lower version than the project default.
Fix 4: Check Source Root Configuration
IntelliJ only indexes directories marked as source roots. If a directory is not marked, its classes are invisible to the IDE.
- Go to File > Project Structure > Modules.
- Select the module and click the Sources tab.
- Verify that
src/main/javais marked as Sources (blue folder). - Verify that
src/test/javais marked as Tests (green folder). - If annotation processors generate code to
target/generated-sources/annotations, mark it as Generated Sources Root.
For Maven projects, reimporting usually sets these correctly. For manually configured projects, you may need to set them yourself.
Fix 5: Regenerate Generated Sources
If the unresolved symbols come from code generators like Lombok, MapStruct, or Dagger, the generated sources may not exist yet.
Then in IntelliJ:
- Go to Settings > Build, Execution, Deployment > Compiler > Annotation Processors.
- Ensure Enable annotation processing is checked.
- Reimport the project.
For Lombok specifically, install the Lombok IntelliJ plugin (Settings > Plugins > search "Lombok") in addition to having the Lombok dependency in your build file.
Fix 6: Delete .idea and Reimport (Nuclear Option)
If nothing else works, remove IntelliJ's project configuration entirely:
Then reopen the project in IntelliJ by opening the pom.xml or build.gradle file as a project. IntelliJ will create a fresh .idea directory with a clean project configuration.
This loses any custom run configurations, code style overrides, and inspection profiles that were stored in .idea. If your team checks .idea files into version control, restore them from Git after the reimport.
Diagnostic Checklist
Use this quick reference to work through the issue systematically:
| 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 from the top and stop at the step that fixes the issue.
Common Pitfalls
- Jumping straight to "Invalidate Caches": This triggers a full re-index that takes minutes. In most cases, a simple reimport (step 1) fixes the problem in seconds.
- Ignoring the background indexing bar: After invalidating caches or reimporting, IntelliJ needs time to re-index. Checking for errors before indexing completes gives false results. Wait for the progress bar at the bottom to finish.
- Using "Build > Build Project" instead of reimporting: Building the project does not update IntelliJ's dependency model. If the issue is a missing dependency, building will not help. Reimporting is the correct action.
- Multiple JDKs installed without specifying which one: If you have JDK 8, 11, and 17 installed, IntelliJ may pick a different one than your build tool. Explicitly set the SDK in Project Structure.
- Lombok without the IntelliJ plugin: The Lombok dependency in
pom.xmlenables compilation, but IntelliJ needs the Lombok plugin to understand@Getter,@Setter, and other annotations during indexing. Without the plugin, every Lombok-generated method shows as unresolved. - Trusting the red squiggles over the build tool: If
mvn clean installsucceeds, the code is correct. The IDE is wrong. This distinction matters when deciding whether to change code or fix IDE configuration.
Summary
- "Cannot resolve symbol" with successful compilation means IntelliJ's project model is out of sync with the build tool.
- Start with a Maven/Gradle reimport, which fixes the majority of cases.
- Check the Project SDK and language level if newer Java features are unresolved.
- Invalidate caches only when reimporting does not help, as it triggers a full re-index.
- For generated sources (Lombok, MapStruct), run the build to generate code and enable annotation processing in IntelliJ settings.
- As a last resort, delete the
.ideadirectory and reimport the project from scratch. - Always wait for indexing to complete after any of these actions before evaluating the result.

