Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java: cannot find symbol and how to read the three-line error

javac names the symbol and the scope it searched. Read both, then fix spelling, scope, imports, annotation processing, or the classpath in that order.

~5 min readJava
#java#javac#compilation#classpath
Bold white headline reading CANNOT FIND SYMBOL with a Java compiler crate pointing an amber caret at an empty cyan slot.

Introduction & Quick Summary

javac stops a build with three lines that carry all the information needed:

code
Main.java:12: error: cannot find symbol System.out.println(userName); ^ symbol: variable userName location: class Main

The caret points at the exact token the compiler could not resolve, symbol: names what it was looking for and what kind of thing it expected, and location: names the scope it searched. The error is never about a value being wrong; it is about a name with no declaration visible at that point.

Quick answer: read the three lines in reverse order. location: class Main tells you where the search happened, symbol: variable userName tells you the kind of name, and the caret tells you the spelling that failed. Then check, in this order: spelling and case, whether the declaration is above the use, whether the name is in scope, whether the import or classpath provides it.

java
// broken class Main { public static void main(String[] args) { System.out.println(userName); // declared later in another method } } // fixed: declare in a visible scope, or pass it in class Main { static String userName = "ada"; public static void main(String[] args) { System.out.println(userName); } }

Add -Xdiags:verbose when the short form is not enough; it restates the error as a full sentence naming the missing member and the type it was looked up on.

Core Mechanics, Signatures & Reproducible Setup

javac resolves every identifier through a fixed order: local variables and parameters in the innermost block, then members of the enclosing class (including inherited ones), then single-type imports, then the same package, then on-demand imports, then java.lang. Anything not found in that chain is cannot find symbol.

The three parts of the message map directly onto that chain:

Message partWhat it is telling youWhere to look
symbol: variable xa field, local, or parameter namedeclaration and scope
symbol: method f(int)a method with that parameter listsignature, receiver type, imports
symbol: class Fooa type nameimport, package, classpath
symbol: constructor Foo()a constructor callarguments and declared constructors
location: class Mainthe type searchedinheritance and static context
location: package com.acmea package-qualified lookupdependency present on classpath

Reproduce every flavour in one file:

java
package demo; import java.util.ArrayList; class Main { static void greet() { System.out.println(name); } // variable not in scope public static void main(String[] args) { list.add("x"); // variable list not declared ArrayList<String> real = new ArrayList<>(); real.size(1); // method size() takes no arguments Helper.help(); // class Helper not imported or absent } }

Four errors, four different symbols, one file. Compiling it takes a second, which is faster than guessing which of the four causes matched your case.

Key Comparison Table

The neighbouring errors are easy to confuse with cannot find symbol, and they mean different things:

ErrorMeaningTypical fix
cannot find symbolname exists nowhere in scopeimport, declare, or fix the spelling
package X does not existthe package itself is unavailableadd the dependency to the classpath
incompatible typesboth names resolved, assignment failsconvert or change the declared type
method does not overridethe parent method is gone or differsmatch the parent signature
non-static variable x cannot be referenced from a static contexta field reached from a static methodmake it static or pass an instance
class file for X not founda dependency of a dependency is missingadd the transitive dependency

The symbol kind narrows the cause quickly:

symbol: lineMost likely causeFirst check
variable xtypo, or declaration in another methodscope and spelling
method f(int,String)wrong argument types or wrong receiverthe receiver's declared methods
class Foomissing import or missing jargrep the jar, then the imports
constructor Foo(int)no matching constructor existsgenerated constructor (Lombok, records)
variable LOGstatic import missingimport static line

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Annotation processing that did not run

Lombok's @Slf4j and @Data generate members during compilation. If the processor is not on the processor path, every generated name becomes a symbol error at once:

xml
<!-- Maven: processor path, separate from the compile classpath --> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path><groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId><version>1.18.36</version></path> </annotationProcessorPaths> </configuration> </plugin>
kotlin
// Gradle Kotlin DSL dependencies { compileOnly("org.projectlombok:lombok:1.18.36") annotationProcessor("org.projectlombok:lombok:1.18.36") }

Declaring Lombok as compileOnly without annotationProcessor is the single most common cause of "cannot find symbol: method getEmail()" on a class that visibly has @Getter.

2. Static context and instance members

java
class Config { private String env = "prod"; // instance field static String current() { return env; } // error: non-static variable env static String fixed(Config c) { return c.env; } }

The compiler names the symbol correctly; the fix is a parameter, a static field, or making the method non-static. The message non-static variable ... cannot be referenced from a static context is a variant of the same resolution failure and is often reported together with cannot find symbol when the field is private to another class.

3. Use before declaration inside a block

java
if (ready) { String label = "ok"; } System.out.println(label); // cannot find symbol: variable label

Java has no block hoisting. A variable declared inside braces dies at the closing brace, and a for loop's counter is not visible after the loop. This shows up constantly when a refactor moves a declaration into a nested block.

4. The dependency is present but the package changed

The Jakarta migration made this the most common new cause in 2024 and later:

java
import javax.servlet.http.HttpServlet; // gone in Spring Boot 3 / Jakarta EE 9+ import jakarta.servlet.http.HttpServlet; // correct

cannot find symbol: class HttpServlet with the jar on the classpath almost always means the package prefix changed, not that the dependency is missing. Check with jar tf ~/.m2/.../jakarta.servlet-api-6.0.0.jar | grep HttpServlet.

5. Classpath and module path discipline

bash
javac -Xdiags:verbose -cp "lib/*:build/classes" -d build src/Main.java java -cp "lib/*:build/classes" Main

Quoting lib/* lets the shell pass it through; an unquoted glob expands to the files and javac treats each as a source file. On the module path the equivalent is --module-path plus --add-modules, and a class that exists in an unexported package produces cannot find symbol even though the jar is loaded.

6. Read the caret, not the word

Long lines truncate the excerpt and the caret can point at a comma. Compile the single file directly rather than through Maven or Gradle to get the clean message:

bash
javac -Xdiags:verbose -d /tmp/out src/main/java/com/acme/Main.java 2>&1 | head -20

A symbol error that only appears in a full build but not for a single file is a classpath difference, and the single-file compile is how you prove it.

Common Compilation/Runtime Mistakes & Version Gotchas

Importing the wrong List. java.awt.List and java.util.List both exist; an IDE auto-import picks the first alphabetically and the compiler then reports cannot find symbol: method add(String) on a type that has no such method. Read location: — it names the resolved type.

Adding a dependency to fix a symbol error without reading the symbol. A missing import and a missing jar produce the same first line and need opposite fixes. symbol: class X plus package X does not exist means the jar; symbol: class X alone with the jar present means the import.

Trusting the IDE's incremental compiler. IntelliJ and Eclipse keep their own output trees; a stale one reports errors for code that compiles on CI and hides errors that CI reports. mvn -o clean compile or ./gradlew clean compileJava is the ground truth.

Version differences. Annotation processor discovery became an explicit choice in JDK 23: a build that relied on processors found on the classpath must now be invoked with -proc:full, and Gradle or Maven plugins that pass no processor path start reporting generated symbols as missing. --release has replaced -source/-target since JDK 9 and, unlike the pair, also restricts the API to that release, which can hide a symbol that exists only in a newer JDK. Records and sealed types require a --release of at least 16 and 17 respectively, so a lower release produces symbol errors for otherwise valid syntax.

Comparing two builds. When the error appears on CI only, diff the compile command lines before touching code. The text diff tool over mvn -X output or gradle --info logs shows the classpath delta in one view, and the regular expression tester helps extract every import of a migrated package so the javax to jakarta sweep is complete rather than partial.

Reference material: javac command reference, JDK 21 and java launcher reference.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.