[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 resolve and IDEA cache.
Step 1: Try to re-index your project files
Exit IDEA, delete the .idea
folder, and 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.
1 | # delete git ignored files |
If the “Cannot resolve symbol” errors are gone, the problem is solved. Otherwise, you can go to the next step.
Step 2: Ensure Maven dependency resolve correctly
You should ensure that your project’s Maven dependencies are resolved correctly. Otherwise, IDEA can’t index project files successfully.
To check that Maven dependencies are resolved correctly, you can execute the following command to check it:
1 | 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 commands again.
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:
1 | 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.
1 | <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:
1 | # force update the dependencies |
After you pass mvn clean package
. You can try to do step 1 to re-index your project files. If the “Cannot resolve symbol” errors are gone, the problem is solved. Otherwise, you can go to the next step.
Step 3: 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”.
If the “Cannot resolve symbol” errors are gone, the problem is solved. Otherwise, You can try to do step 1 to re-index your project files again.