All posts

Fix Missing Import in Java

Resolve Java import errors and classpath issues, covering Maven/Gradle dependencies, package naming, and static imports.

Missing Import Errors in Java

Java's cannot find symbol or package does not exist errors mean the compiler can't locate a class. This is either a missing import statement or a missing dependency.

Add the Import

// Error: cannot find symbol: variable List
// Fix:
import java.util.List;
import java.util.ArrayList;

List<String> items = new ArrayList<>();

Missing Maven Dependency

<!-- pom.xml -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>
mvn clean install

Missing Gradle Dependency

// build.gradle
implementation 'com.google.code.gson:gson:2.10.1'
gradle build --refresh-dependencies

Static Imports

For static methods and constants:

// Instead of: Assert.assertEquals(...)
import static org.junit.jupiter.api.Assertions.assertEquals;
assertEquals(expected, actual);

// For constants
import static java.lang.Math.PI;
double area = PI * r * r;

Common Confusions

// java.util.Date vs java.sql.Date
import java.util.Date;  // Most of the time you want this

// Jakarta EE vs javax (Jakarta EE 9+)
// Old: import javax.servlet.http.HttpServletRequest;
// New: import jakarta.servlet.http.HttpServletRequest;

IDE Auto-Import

IntelliJ: Alt+Enter on the unresolved symbol.

VS Code: Ctrl+. with the Java extension.

Bugsly captures ClassNotFoundException and NoClassDefFoundError at runtime, which indicates imports that resolved at compile time but are missing from the runtime classpath.

Try Bugsly Free

Track up to 100 issues per month on the free plan, with unlimited events and no credit card required.

Get Started Free