Course contentsShow
← Java
Lesson 14 of 2,8701. Java Setup and First ProgramFree lesson

Understanding Compilation Errors

Reading and interpreting common javac error messages like syntax errors and missing semicolons.

Understanding Compilation Errors

What you'll learn: How to read and fix the error messages that javac shows when your code has problems.

What Are Compilation Errors?

When you run javac YourFile.java, the compiler checks your code before turning it into bytecode. If it finds mistakes—like typos, missing punctuation, or broken rules—it stops and prints compilation errors. These aren't crashes; they're the compiler's way of saying "I can't understand this part yet."

Think of it like a spell-checker that won't let you submit a document until you fix the red underlines.

Reading Error Messages

Error messages from javac follow a pattern:

HelloWorld.java:5: error: ';' expected
    System.out.println("Hello, World")
                                      ^
1 error

What this tells you:

  • File and line number: HelloWorld.java:5 (the problem is on line 5)
  • Error type: ';' expected (you forgot a semicolon)
  • Location marker: The ^ symbol points to where the compiler got confused

Common Errors You'll See

Missing semicolon:

System.out.println("Hello")  // Error: ';' expected

Misspelled keywords or methods:

pubic static void main(String[] args)  // Error: 'pubic' is not recognized

Mismatched braces:

public class Test {
    public static void main(String[] args) {
        System.out.println("Hi");
    // Missing closing brace for the class

How to Fix Them

  1. Read the line number and look at that line in your code
  2. Check the line above too—sometimes the real mistake is earlier
  3. Fix one error at a time, then recompile
  4. Look for patterns: missing semicolons and typos are most common at first

Key Takeaway: Compilation errors are your friend—they catch mistakes before your program runs, and the error messages tell you exactly where to look and what's wrong.