IntelliJ
Java
troubleshooting
code editor
import errors

IntelliJ "Cannot resolve symbol" on import: how to fix it

Codemia Knowledge Hub ·

IntelliJ IDEA shows "Cannot resolve symbol" when it cannot find the definition of a class or package you reference, often on an import line. The usual causes are a dependency missing from the build configuration, a wrong or missing project SDK, corrupted caches, an excluded module or source folder, or a typo in the import.

1. The dependency is not configured

The library you import is not set up correctly in your build system (Maven or Gradle).

  • Maven: Make sure pom.xml lists the correct dependencies. After editing it, right-click pom.xml in IntelliJ and select "Maven" -> "Reload".
  • Gradle: Check that build.gradle includes the dependencies you need, then click "Reload All Gradle Projects" in IntelliJ.

2. The project SDK is wrong or missing

Go to "File" -> "Project Structure" -> "Project" and make sure the correct SDK is selected. If it is not listed, add it with "New".

3. Caches or indexes are corrupted

Go to "File" -> "Invalidate Caches / Restart..." and choose "Invalidate and Restart" to clear the caches and restart the IDE.

4. Sources or modules are excluded

Sources or modules can be accidentally marked as excluded.

  • Open "File" -> "Project Structure" -> "Modules" and check that module paths are correct and not marked as "Excluded".
  • In the "Sources" tab of the module settings, verify the source directories and dependencies.

5. The import statement is wrong

Check the spelling of the import, and make sure you import the class from the correct package and version.

Example: StringUtils from Apache Commons Lang

Say your project uses Apache Commons Lang:

java
1import org.apache.commons.lang3.StringUtils;
2
3public class Example {
4    public static void main(String[] args) {
5        String text = "Hello World!";
6        System.out.println(StringUtils.reverse(text));
7    }
8}

If StringUtils shows "Cannot resolve symbol", work through these steps.

First, check that commons-lang3 is in your pom.xml:

xml
1   <dependency>
2       <groupId>org.apache.commons</groupId>
3       <artifactId>commons-lang3</artifactId>
4       <version>3.12.0</version>
5   </dependency>

Then:

  1. Reload the Maven project so the dependency is imported.
  2. Check the project SDK in Project Structure.
  3. If the error remains, invalidate the caches and restart IntelliJ.

Causes and fixes at a glance

Issue/ScenarioCauseSolution
Dependency not resolvedDependency not in build fileAdd to pom.xml or build.gradle and reload the project
SDK issuesSDK path incorrect or absentSet correct SDK in "Project Structure"
Cache corruptionProject indices are corruptedUse "Invalidate Caches / Restart"
Module not recognizedModule path is incorrect/excludedEnsure correct module path and check module settings
Wrong import statementTypo in import statementCorrect the statement or ensure correct version/package is being referenced

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms