IntelliJ inspection gives "Cannot resolve symbol" but still compiles code
There are a few possible reasons why IntelliJ might show a "Cannot resolve symbol" error while still being able to compile your code.
There are a few possible reasons why IntelliJ might show a "Cannot resolve symbol" error while still being able to compile your code. Here are some things you can try:
- Make sure that you have the necessary dependencies in your project. If you are using a library or framework that is not included in your project, IntelliJ will not be able to resolve the symbols for that library. Add it to your build configuration and sync:
<!-- Maven (pom.xml) -->
<dependency>
<groupId>com.example</groupId>
<artifactId>example-lib</artifactId>
<version>1.0.0</version>
</dependency>Click the Reload All Gradle/Maven Projects button to update the classpath.
-
Check if you have any issues with your project configuration. In IntelliJ, go to File > Invalidate Caches / Restart and select Invalidate and Restart. This will reset your project configuration and might fix the issue. Additionally, go to Build > Rebuild Project to force a fresh compilation cycle.
-
Make sure that you are using the correct package for the symbol you are trying to use. If you have imported the wrong package, IntelliJ will not be able to find the symbol.
// Incorrect
import java.util.Date;
// Correct
import java.time.LocalDate;Press Alt + Enter (or Option + Enter on macOS) to auto-import the correct class.
- If you are using an external library or dependency, make sure that it is properly configured in your project. You may need to add the library to your classpath or import it in your code. Also, verify that your module SDK and language level are set correctly under File > Project Structure > Modules.
Why compilation still succeeds: IntelliJ's inspection engine and the actual Java compiler (javac) operate independently. The IDE may flag symbols as unresolved due to stale caches, offline indexing, or temporary sync delays, while the compiler uses the last successfully built artifacts or incremental build state. Rebuilding or syncing usually aligns both.
I hope these suggestions help! Let me know if you have any other questions.