Skip to content

How to Fix 'cannot find symbol' in Java

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix 'cannot find symbol' in Java. We cover key concepts, practical examples, and best practices.

The Problem

You compile a Java file and get:

MyClass.java:5: error: cannot find symbol
  symbol:   method parseInput(String)
  location: class MyClass

The compiler cannot locate a class, method, variable, or type you referenced.

Quick Fix

1. Check imports

Missing imports are the most common cause. If you use List, Map, File, or any class from another package, you must import it.

// Add at the top of the file
import java.util.List;
import java.util.Map;
import java.io.File;

Look at the symbol name and add the corresponding import. Most IDEs show a red underline — press Alt+Enter (Eclipse/IntelliJ) to auto-import.

2. Fix typos and case errors

Java is case-sensitive. String and string are different. Compare your code to the actual class or method name:

// Bad
String name = "hello";
system.out.println(name);  // should be System (capital S)

// Good
System.out.println(name);

Common typos: missing capital letter, wrong spelling, or using the wrong method name.

3. Verify method or variable scope

A symbol must be visible in the scope where you use it:

public class MyClass {
    public void methodA() {
        int x = 10;
    }

    public void methodB() {
        System.out.println(x);  // error: x is not in scope
    }
}

Move the variable to class level or pass it as a parameter:

public class MyClass {
    private int x = 10;  // now visible everywhere in the class

    public void methodA() {
        System.out.println(x);
    }

    public void methodB() {
        System.out.println(x);
    }
}

4. Build the project (multi-file projects)

If ClassA depends on ClassB in the same project, ClassB must be compiled first:

# Compile all sources together
javac src/com/example/*.java -d out/

# Or with Maven
mvn clean compile

# Or with Gradle
./gradlew build

Running the compiler on a single file will fail if that file references classes that have not been compiled yet.

5. Fix the classpath for external libraries

When using JAR files or libraries, include them in the classpath:

javac -cp "lib/*:." MyApp.java
java -cp "lib/*:." MyApp

Make sure the JAR file path is correct and the JAR contains the expected class. Use jar tf library.jar to inspect its contents.

6. Clean stale build artifacts

Old .class files can hide compilation issues:

# Delete all compiled class files
rm -rf out/ target/ *.class

# Rebuild from scratch
javac -d out src/com/example/*.java

Prevention

  • Use an IDE — IntelliJ IDEA or Eclipse catch missing imports and typos as you type.
  • Run mvn clean compile or ./gradlew build regularly, not just at the end.
  • Keep external JARs in a dedicated lib/ folder and use a build tool (Maven/Gradle) to manage dependencies automatically.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro