Skip to content
error: cannot find symbol

error: cannot find symbol

DodaTech 3 min read

error: cannot find symbol means the compiler found a name it cannot resolve. A variable, class, or method doesn’t match any declaration in the current scope.

What It Means

The Java compiler works through your code and maintains a symbol table of all declared names. When it sees an identifier like userName, getTotal(), or ArrayList, it looks up that name in the symbol table. If it can’t find it, compilation stops with cannot find symbol.

Why It Happens

  • You misspelled a variable, method, or class name.
  • You forgot to import a class.
  • You’re trying to access a method or field that doesn’t exist on the type.
  • The variable is out of scope (declared in a different block).
  • You’re using a class from a dependency that isn’t on the compile classpath.
  • You used the wrong case for the identifier (Java is case-sensitive).

How to Fix It

1. Check for typos in the identifier

// BUG: misspelled variable name
String userName = "Alice";
System.out.println(usernName);  // cannot find symbol — usernName != userName

// FIX: use the exact declared name
System.out.println(userName);

2. Add the missing import

// BUG: missing import
public class MyClass {
    public static void main(String[] args) {
        List<String> items = new ArrayList<>();  // cannot find symbol: List
    }
}

// FIX: add import at the top of the file
import java.util.List;
import java.util.ArrayList;

3. Check the method or field exists on the type

String name = "Alice";
int len = name.length();   // OK — length() exists on String
int cap = name.capacity(); // cannot find symbol — String has no capacity()

// FIX: use the correct method for the type
int cap = name.length();   // or use StringBuilder if you need capacity

4. Verify variable scope

// BUG: variable declared inside a block, used outside
if (true) {
    int x = 10;
}
System.out.println(x);  // cannot find symbol — x is out of scope

// FIX: declare in the outer scope
int x;
if (true) {
    x = 10;
}
System.out.println(x);

5. Make sure the class is on the compile classpath

# Compile with the required JAR
javac -cp lib/commons-lang3-3.12.0.jar MyProgram.java

If you miss the -cp argument, symbols from the library will not be found.

6. Use the correct case

// BUG: wrong case
String s = "hello";
System.out.println(s.length);    // cannot find symbol — length is a METHOD

// FIX: Java is case-sensitive
System.out.println(s.length());  // method call  correct
Does 'cannot find symbol' always mean a typo?
Mostly, but not always. It can also mean a missing import, a missing dependency JAR, or a variable being used outside its scope. Read the error line carefully — it tells you the symbol name and the location. The symbol name is often the best clue for the fix.
How do I fix 'cannot find symbol' for a class in the same package?
Classes in the same package don’t need an import. If the compiler still can’t find it, check: (1) both files are in the same directory, (2) the class name matches the filename exactly, and (3) the class is declared public (or you access it from within the same package).
Why does IntelliJ compile but javac on the command line fails?
Your IDE manages the classpath automatically — it knows about all your Maven/Gradle dependencies. On the command line, you must provide -cp (or --class-path) with all required JARs. Use mvn dependency:build-classpath to get the classpath string for manual compilation.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro