Common Beginner Compile Errors
Red error text after javac can feel like the computer is telling you off. It isn't. A compile error is the compiler doing you a favor: it found a problem before your program reached a user. Every message follows the same pattern, and the same eight or so errors cause most beginners' trouble. Learn to read them once and they stop being scary.
How to Read an Error Message
| Part | Meaning |
|---|---|
Errs.java | The file with the problem |
:3: | The line number |
error: ';' expected | What the compiler expected or didn't understand |
The copied line and ^ | Where on the line it noticed the problem |
1 error | How many errors in total |
The Errors You'll Meet Most
Each message below is real javac output from Java 25.
1. ';' Expected
Errs1.java:3: error: ';' expected
System.out.println("Hi")
^Cause: the statement doesn't end with a semicolon. Fix: add ; where the ^ points. Sometimes the ^ points just after the line that's really missing it.
2. Cannot Find Symbol
Errs2.java:4: error: cannot find symbol
System.out.println(totl);
^
symbol: variable totl
location: class Errs2Cause: Java doesn't know the name totl. Usually it's a spelling mistake (totl instead of total), a variable used outside the braces it was declared in, or a missing import. Fix: check the spelling and capital letters of the name shown after symbol:.
3. Package system Does Not Exist
Errs3.java:3: error: package system does not exist
system.out.println("Hi");
^Cause: system with a small s. Java is case-sensitive, so it thinks system is a package name. Fix: write System.
4. Class Is Public, Should Be Declared in a File Named ...
Wrong.java:1: error: class Right is public, should be declared in a file named Right.java
public class Right {
^Cause: the file name and the public class name don't match. Fix: rename the file to Right.java, or rename the class to Wrong.
5. Incompatible Types
Errs5.java:3: error: incompatible types: String cannot be converted to int
int count = "five";
^Cause: a value of one type is put where another type is needed. Here, text goes into an int. Fix: use the right type (int count = 5;), or convert it, for example with Integer.parseInt("5").
6. Variable Might Not Have Been Initialized
Errs6.java:4: error: variable x might not have been initialized
System.out.println(x);
^Cause: a local variable is used before it has a value. Java never guesses a default for local variables. Fix: give it a value when you declare it, such as int x = 0;.
7. Missing Return Statement
Errs7.java:4: error: missing return statement
}
^Cause: a method promises to return a value (for example static int twice(int n)), but there's a path where it ends without return. Fix: add return result; before the closing brace.
8. Reached End of File While Parsing
Errs8.java:4: error: reached end of file while parsing
}
^Cause: a { was never closed, so the file ended while Java was still inside a class or method. Fix: count your braces. A good editor highlights the matching brace when the cursor is next to one.
Fix a Broken Program
This is the corrected version of a program that had three of the errors above: a missing semicolon, system with a small s, and a misspelled variable name.
public class Receipt {
public static void main(String[] args) {
int items = 4;
int pricePerItem = 60;
int total = items * pricePerItem;
System.out.println("Items: " + items);
System.out.println("Total: Rs. " + total);
}
}Items: 4
Total: Rs. 240How it works
- Line 3 originally read
int items = 4with no semicolon, which gave';' expected. - Line 6 originally used
system.out, which gavepackage system does not exist. - Line 5 originally used
pricePerltem(with a lowercase L instead of an I), which gavecannot find symbol. - With all three fixed,
javacprints nothing and the program runs.
Try it yourself: put back one of the mistakes at a time, compile, and match the message to the list above.
Not a Compile Error: Launcher Messages
Some errors appear only when you run java, after compiling succeeded. They come from the launcher, not from javac:
| Message | Usual cause |
|---|---|
Error: Could not find or load main class Bill | Wrong class name, .class added to the command, or the wrong folder |
Error: Main method not found in class Fare | The class has no main method, or main is spelled Main or is private |
Common Mistakes
Reading only the last line. 1 error tells you nothing. The useful part is the first line: file, line number and message.
Changing code at random until it compiles. Read the message, find the line, fix the one thing it describes. Random changes create new errors.
Ignoring warnings. Lines that say warning: don't stop compilation, but they often point to a real bug. Read them too.
Practice
- Easy: For each message, name the likely fix:
';' expected,cannot find symbol,reached end of file while parsing. - Harder: Write a method
static int discount(int price)that returns 10% of the price, but leave out thereturnline on purpose. Compile it, read the error, then fix it.
Key Takeaways
Was this tutorial helpful?