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.

Introduction & Quick Summary
javac stops a build with three lines that carry all the information needed:
Main.java:12: error: cannot find symbol
System.out.println(userName);
^
symbol: variable userName
location: class MainThe 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.
// 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 part | What it is telling you | Where to look |
|---|---|---|
symbol: variable x | a field, local, or parameter name | declaration and scope |
symbol: method f(int) | a method with that parameter list | signature, receiver type, imports |
symbol: class Foo | a type name | import, package, classpath |
symbol: constructor Foo() | a constructor call | arguments and declared constructors |
location: class Main | the type searched | inheritance and static context |
location: package com.acme | a package-qualified lookup | dependency present on classpath |
Reproduce every flavour in one file:
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:
| Error | Meaning | Typical fix |
|---|---|---|
cannot find symbol | name exists nowhere in scope | import, declare, or fix the spelling |
package X does not exist | the package itself is unavailable | add the dependency to the classpath |
incompatible types | both names resolved, assignment fails | convert or change the declared type |
method does not override | the parent method is gone or differs | match the parent signature |
non-static variable x cannot be referenced from a static context | a field reached from a static method | make it static or pass an instance |
class file for X not found | a dependency of a dependency is missing | add the transitive dependency |
The symbol kind narrows the cause quickly:
symbol: line | Most likely cause | First check |
|---|---|---|
variable x | typo, or declaration in another method | scope and spelling |
method f(int,String) | wrong argument types or wrong receiver | the receiver's declared methods |
class Foo | missing import or missing jar | grep the jar, then the imports |
constructor Foo(int) | no matching constructor exists | generated constructor (Lombok, records) |
variable LOG | static import missing | import 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:
<!-- 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>// 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
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
if (ready) {
String label = "ok";
}
System.out.println(label); // cannot find symbol: variable labelJava 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:
import javax.servlet.http.HttpServlet; // gone in Spring Boot 3 / Jakarta EE 9+
import jakarta.servlet.http.HttpServlet; // correctcannot 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
javac -Xdiags:verbose -cp "lib/*:build/classes" -d build src/Main.java
java -cp "lib/*:build/classes" MainQuoting 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:
javac -Xdiags:verbose -d /tmp/out src/main/java/com/acme/Main.java 2>&1 | head -20A 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 documentationThe Java documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesSpring Boot: No qualifying bean of type available
The context could not satisfy a constructor argument. Scan boundaries, missing stereotypes, ambiguous candidates and test slices are the usual reasons.
Method Scope in Java: How Local Variables Work
Understand how variables behave inside Java methods: local variable boundaries, block scope, parameter scope, shadowing, and lifetime implications.
Java: HikariPool connection is not available, request timed out
The HikariCP pool ran out of connections for the whole connectionTimeout. Read the counters, find leaks or slow queries, then size the pool.
Java: DateTimeParseException: Text could not be parsed
The formatter pattern and the input disagree. Check the index in the message, the locale, and whether the text needs a ZonedDateTime instead.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.