You compile a short program and javac stops with a message that seems to say very little:
Main.java:4: error: cannot find symbol
System.out.println(hour * 365);
^
symbol: variable hour
location: class Main
1 errorerror: cannot find symbol means the compiler met a name, a symbol, that it doesn't know: a variable, a method or a class that, as far as Java can see, doesn't exist at that point in your code. It's the most common Java compile error for beginners, and the message tells you more than it seems: the symbol: line names exactly what's missing, and the location: line says where Java looked. Here, the variable is called hours, but the code uses hour. This article shows how to read those two lines and fix the eight usual causes.
Try it yourself
Save this as Main.java and run javac Main.java:
public class Main {
public static void main(String[] args) {
int hours = 3;
System.out.println(hour * 365);
}
}You'll get the error above. Change hour to hours, compile again, and run java Main: it prints 1095.
How to read "cannot find symbol"
The message has four parts, and the last two are the important ones:
Main.java:4: the file and line.- The
^mark: the exact spot. It points at the start of the unknown name. symbol: variable hour: what Java couldn't find, and what kind of thing it thought it was: avariable, amethodor aclass.location: class Main: where Java looked for it. For a method called on an object, this line names the object and its type.
So ask two questions: is the name spelled exactly like the place where I created it? And: can Java see that place from here?
Cause 1: a typo or a capital letter
The example above. Java names are case-sensitive: hours, hour, Hours and HOURS are four different names. The same goes for built-in names:
string name = "Alex";
system.out.println(name);Main.java:3: error: cannot find symbol
string name = "Alex";
^
symbol: class string
location: class Main
Main.java:4: error: package system does not exist
system.out.println(name);
^
2 errorsString and System are classes, and Java class names start with a capital letter. Notice that the lowercase system gives a different message: Java guesses that system.out might be a package name, and says the package doesn't exist. Same mistake, same fix.
Cause 2: a misspelled method
When the unknown name is a method, the symbol: line shows method and the types of the arguments:
String name = "Alex";
System.out.println(name.lenght());Main.java:4: error: cannot find symbol
System.out.println(name.lenght());
^
symbol: method lenght()
location: variable name of type String
1 errorThe location: line is the useful one here: Java looked for lenght() in name, which is a String, and String has no such method. The correct spelling is length().
The same with a capital letter in a built-in method:
Main.java:3: error: cannot find symbol
Integer.parseint("5");
^
symbol: method parseint(String)
location: class Integer
1 errorIt's Integer.parseInt, with a capital I. In Java, method names use camelCase: each new word starts with a capital letter.
Cause 3: a missing import
Classes outside the java.lang package must be imported. The classic one is Scanner:
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
}
}Main.java:3: error: cannot find symbol
Scanner scanner = new Scanner(System.in);
^
symbol: class Scanner
location: class Main
Main.java:3: error: cannot find symbol
Scanner scanner = new Scanner(System.in);
^
symbol: class Scanner
location: class Main
2 errorsTwo errors for one mistake: Java can't find Scanner as a type, and again after new. Add the import above the class:
import java.util.Scanner;Other classes beginners often forget to import: ArrayList, HashMap, List, Random (all in java.util), and LocalDate (in java.time). String, Math, Integer and System never need an import, because they live in java.lang.
Cause 4: a variable used outside its block
A variable exists only inside the { } block where it was declared. That's called its scope. Outside the block, Java can't see it:
int hours = 3;
if (hours > 2) {
String label = "a lot";
}
System.out.println(label);Main.java:7: error: cannot find symbol
System.out.println(label);
^
symbol: variable label
location: class Main
1 errorlabel was created inside the if block and disappeared at its closing }. Declare it before the block and give it a value inside:
String label = "a little";
if (hours > 2) {
label = "a lot";
}
System.out.println(label);The same rule applies to loops. A variable declared inside a for loop, or the loop counter i itself, doesn't exist after the loop ends.
Cause 5: a method that doesn't exist (yet)
int total = add(2, 3);Main.java:3: error: cannot find symbol
int total = add(2, 3);
^
symbol: method add(int,int)
location: class Main
1 errorsymbol: method add(int,int) shows what Java searched for: a method called add that takes two int values. Either the method isn't written yet, it's in a different class, or its name is different. Write it, or call it through the right class.
If the method exists but you call it with the wrong number or types of arguments, you get a different error that lists what it expected:
Main.java:4: error: method add in class Main cannot be applied to given types;
System.out.println(add(2));
^
required: int,int
found: int
reason: actual and formal argument lists differ in length
1 errorCause 6: length, length() and size()
Java uses three different ways to ask "how many", and mixing them up gives cannot find symbol:
ArrayList<String> names = new ArrayList<>();
System.out.println(names.length);
int[] nums = {1, 2};
System.out.println(nums.size());Main.java:6: error: cannot find symbol
System.out.println(names.length);
^
symbol: variable length
location: variable names of type ArrayList<String>
Main.java:8: error: cannot find symbol
System.out.println(nums.size());
^
symbol: method size()
location: variable nums of type int[]
2 errorsThe rule to remember:
- Arrays use
length, without brackets:nums.length. - Strings use
length(), with brackets:name.length(). - Lists (
ArrayList) usesize():names.size().
The location: line tells you which one you have: of type int[] is an array, of type ArrayList<String> is a list.
Cause 7: a class in another file
If your program uses a class from another file, like Dog.java, Java needs to find that file:
Main.java:3: error: cannot find symbol
Dog d = new Dog();
^
symbol: class Dog
location: class MainCheck these three things:
- The file is named exactly like the class,
Dog.java, capital letter included. - It's in the same folder as
Main.java(or in the right package folder). - You compile with
javac Main.javafrom that folder. In our test with JDK 21,javacfoundDog.javanext toMain.javaby itself and the program ran. But runningjava Main.javadirectly, the single-file shortcut, gave this error, because that mode compiles one file only.
Cause 8: your editor's wording
Different tools describe the same problem in their own words. You may see:
java: cannot find symbol
Cannot resolve symbol 'hour'
hour cannot be resolved to a variableThe first is the same javac message as shown by IntelliJ IDEA's build window. The second is the red-underline wording in IntelliJ, and the third is how Eclipse says it (Eclipse also says cannot be resolved to a type for classes). All three have the same causes and the same fixes.
Errors that look alike
package system does not exist: a lowercase class name used like a package (cause 1).method X in class Y cannot be applied to given types: the method exists, but the arguments don't match (cause 5).incompatible types: String cannot be converted to int: the name exists, but the value has the wrong type. See String cannot be converted to int.class, interface, enum, or record expected: usually a brace in the wrong place. See reached end of file while parsing.
Wrong fixes that don't help
- Declaring the variable again where the error is. A new
String label;before theprintlnremoves the error and prints the wrong thing (or causesmight not have been initialized). Fix the scope instead. - Importing everything with
import java.util.*;for every error. It helps only when a class fromjava.utilis missing. A misspelled name stays misspelled. - Fixing only the first of several errors and giving up. One missing import often causes several
cannot find symbolerrors. Fix the first, compile again, and the count usually drops.
A debugging walkthrough
javac reports five cannot find symbol errors in a 40-line program:
- Start with the first error. Later ones are often caused by it.
- Read the
symbol:line. Is it aclass, amethodor avariable? - For a class: is the capital letter right, and is the import there?
- For a method: compare with the documentation or the definition, letter by letter. Check the
location:type. - For a variable: find where it's declared. Is that inside a
{ }block that has already closed? - Compile again after each fix.
Practice
Predict the symbol: line for each.
Math.sqroot(16)ArrayList<Integer> list = new ArrayList<>();without any import.for (int i = 0; i < 3; i++) { } System.out.println(i);
Answers. (1) symbol: method sqroot(int), location class Math: the method is Math.sqrt. (2) symbol: class ArrayList: add import java.util.ArrayList;. (3) symbol: variable i: the loop counter exists only inside the loop.
Frequently asked questions
What does "cannot find symbol" mean in Java?
The compiler found a name it doesn't know at that point: a variable, method or class that is misspelled, not imported, not written yet, or out of scope.
What is a symbol in Java?
Any name in your code: a variable, a method, a class or a package. The symbol: line says which kind Java was looking for.
How do I fix cannot find symbol for Scanner?
Add import java.util.Scanner; at the top of the file, above the class.
Why does Java say cannot find symbol when the variable exists?
It exists somewhere else: inside a block that has already closed, in another method, or with a different spelling or capital letter.
Is "cannot find symbol" a compile error or a runtime error?
A compile error. javac stops, and no program runs until you fix it.
A quick checklist
- Read the
symbol:line: is it a variable, method or class? - Compare the spelling, including capitals, with where it's declared.
- For classes, check the import and the file name.
- For variables, check that you're inside the block that declared them.
- For "how many": arrays
length, stringslength(), listssize(). - Fix the first error, then compile again.
For a method that works on any error, read how to read an error message. The official Java tutorial on variables explains naming rules.
