[IntelliJ IDEA] Solved Cannot resolve symbol
The problem “cannot resolve symbol” means IDEA hasn’t successfully indexed your project code and dependent libraries.
There are two common problems that cause the “cannot resolve symbol” problems:
- Maven dependency configuration problems.
- IDEA problems.
Maven Configuration Problems
Ensure Maven dependency resolve correctly
Your project’s Maven dependencies must be resolved correctly. Otherwise, IDEA can’t index project files successfully.
To check that Maven dependencies are resolved correctly, you can execute the following command:
mvn -Dmaven.test.skip=true clean package |
If you can’t pass the above command, then there is something wrong with your Maven configuration. You can update your pom.xml
and run the above command again.
After you passed the above command by updating your pom.xml
, you need to reload the maven project to download missing dependencies. Right click the pom.xml
in IDEA -> Maven -> Reload project. If the “cannot resolve symbol” errors are gone, the problem is solved.
If you have passed the above command and reloaded the project but there are still the “cannot resolve symbol” problems in IDEA, then it’s IDEA problems not Maven. You can go to the “Problems with IDEA” section.
Common problems and solutions with Maven
Problem 1: The dependency cannot be found
This means that the dependency cannot be found from the remote Maven repository.
Error Information:
Could not resolve dependencies for project {your_project}: Failed to collect dependencies at {groupId}:{artifactId}.jar:{version} |
Solutions:
Check if the groupId, artifactId or version of the dependency is correct.
Check if there is an unofficial repository defined in the project’s pom.xml or ~/.m2/settings.xml, try using Maven central repository.
<repository> |
Problem 2: dependency conflict
This means that multiple versions of a dependency are added or indirectly added to the Maven project. A dependency can only exist in one version. The actual version used causes the referenced method or class to be unable to be found.
Error Information:
NoClassDefFoundError
, ClassNotFoundException
, or NoSuchMethodError
Solutions
Use the “Maven Helper” plugin to check if there is a dependency conflict. You can try excluding unwanted versions by right-clicking on the version in the Maven Helper plugin window.
If your Maven project has a parent project, you also need to check whether the version defined in the parent project is compatible with your current project.
More tips
If you pom.xml is correct, but you still can’t pass mvn clean package
. You can try to force update dependencies:
# force update the dependencies |
Problems with IDEA
Try to re-index your project files
Close the project or exit IDEA, then delete the .idea
folder, then reopen project or restart IDEA. When there is no .idea
folder in your project root path, IDEA will re-index project files automatically.
In addition to the .idea
folder, if your project is a git repository, it’s recommended to delete all file ignored by git.
# delete git ignored files |
After you re-indexed the project (delete .idea
), if the “cannot resolve symbol” errors are gone, the problem is solved.
If the “cannot resolve symbol” errors still exist, you can try rebuild project. In top menu bar, Build -> Rebuild project.
If you have re-indexed the project (delete .idea
) and rebuilt the project but the “cannot resolve symbol” errors still exist, you can go to the next step “Invalidate IDEA cache”.
Invalidate IDEA cache
IDEA cache also affects IDEA index project files. If your Maven configurations are right, but there are still “cannot resolve symbol” problems. You can try to Invalidate Caches:
File -> Invalidate Caches -> checked “clear file system cache and Local History”, and then click “Invalidate and Restart”.
Re-enter IDEA and waiting for “Update indexes” to complete. After “Update indexes” is done. Then we need rebulid the project:
Build -> click “Rebuild Project”.
After invalidated IDEA cache, if the “cannot resolve symbol” errors are gone, the problem is solved. Otherwise, You can try to re-index your project files again.