Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Java

Java: reached end of file while parsing, and class, interface, enum, or record expected

· 8 min read

Java programs are built from blocks inside blocks: a class, methods inside it, ifs and loops inside those. Every block starts with { and ends with }. Lose one brace, or add one too many, and the compiler gives you messages that seem to have nothing to do with braces, and often point at the wrong line.

This guide covers the brace errors beginners meet most, what each one means and how to find the brace that's really at fault. We compiled every example with Java 17, 21 and 25, because, as you'll see, the newer versions word some of these errors very differently.

reached end of file while parsing

Main.java
public class Main {
    public static void main(String[] args) {
        int hours = 3;
        if (hours > 2) {
            System.out.println("That's a lot of screen time.");

        System.out.println("Done.");
    }
}
Terminal
Main.java:9: error: reached end of file while parsing
}
 ^
1 error

What it means: the compiler read the whole file and got to the end while a block was still open. Parsing is the compiler reading your code to work out its structure, and it ran out of file before the structure was complete. In other words: there's at least one { without its }.

Why it points at the wrong place: the missing brace belongs after line 5, to close the if. But the compiler can't know that. It happily reads the } on line 8 as the end of the if, the one on line 9 as the end of main, and then the file ends while the class is still open. So it reports the very last line, the only place where it's sure something is missing.

The fix: find the block that was never closed and add its } where it belongs:

java
        if (hours > 2) {
            System.out.println("That's a lot of screen time.");
        }
        System.out.println("Done.");

This message is the same in Java 17, 21 and 25.

class, interface, enum, or record expected

The opposite problem: one } too many. Here, an extra brace closes the class before the second method:

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println(average(7, 2));
    }
    }

    static double average(int total, int count) {
        return (double) total / count;
    }
}

The } on line 5 ends the class Main. Everything after it, the method average and the last }, is now outside any class, and that's where this message comes from. But what exactly the compiler says depends on your Java version.

Java 17 says it plainly, once for the method and once for the stray brace:

Terminal (Java 17)
Main.java:7: error: class, interface, enum, or record expected
    static double average(int total, int count) {
           ^
Main.java:9: error: class, interface, enum, or record expected
    }
    ^
2 errors

That is: at the top level of a file, outside any class, Java only expects the start of a new class, interface, enum or record. A method isn't allowed there. (Older versions from before records were added, like Java 8 and 11, say class, interface, or enum expected.)

Java 21 adds a confusing first line:

Terminal (Java 21)
Main.java:7: error: unnamed classes are a preview feature and are disabled by default.
    static double average(int total, int count) {
           ^
  (use --enable-preview to enable unnamed classes)
Main.java:10: error: class, interface, enum, or record expected
}
^
2 errors

Java 25 says this:

Terminal (Java 25)
Main.java:10: error: class, interface, annotation type, enum, record, method or field expected
}
^
1 error

Why the newer versions talk about methods and "unnamed classes": since Java 25, a small program doesn't need a class line at all: a file can contain just methods, starting with void main(). (Java 21 had a preview of the same idea, under the name "unnamed classes".) So when the compiler finds a method outside your class, newer versions no longer say "that's not allowed"; they assume you might be writing one of these class-less programs. For a beginner, the advice is the same in every version: a } too early has closed your class.

The fix: remove the extra } on line 5, so that average is inside the class again.

A method below the class

A related mistake: you add a new method after the class has already ended, below its final }:

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

static void greet() {
    System.out.println("Hello");
}

Here the three versions disagree completely:

Terminal (Java 17)
Main.java:7: error: class, interface, enum, or record expected
static void greet() {
       ^
Terminal (Java 21)
Main.java:7: error: unnamed classes are a preview feature and are disabled by default.
static void greet() {
       ^
  (use --enable-preview to enable unnamed classes)
Terminal (Java 25)
Main.java:1: error: compact source file does not have main method in the form of void main() or void main(String[] args)
public class Main {
^

The Java 25 message is the most puzzling one: it points at line 1 and complains about main, which looks perfectly fine. What happened is that Java 25 treated the whole file as a "compact source file", a class-less program, because of the method outside the class, and then looked for a main at the top level of the file. It didn't find one, because yours is inside the class.

The fix is the same in all three: move the method inside the class, above its last }.

Two more errors from braces in the wrong place

<identifier> expected

Code that should be inside a method, written directly in the class:

Main.java
public class Main {
    System.out.println("Hi");

    public static void main(String[] args) {
    }
}
Terminal
Main.java:2: error: <identifier> expected
    System.out.println("Hi");
                      ^
Main.java:2: error: illegal start of type
    System.out.println("Hi");
                       ^
2 errors

Directly inside a class, Java only expects declarations: fields (variables of the class) and methods. A statement like System.out.println(...) must be inside a method. Move it into main. All three versions report this the same way.

illegal start of expression

A method that starts before the previous one has ended:

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hi");

    static void greet() {
        System.out.println("Hello");
    }
}
Terminal
Main.java:5: error: illegal start of expression
    static void greet() {
    ^
1 error

main is still open when greet begins, and Java doesn't allow a method inside a method. The missing } belongs at the end of main, just before the blank line. Again, the same message in all three versions.

How to find a missing or extra brace

Brace errors are easy to fix once you've found the brace, and the hard part is finding it. These habits help:

  1. Let the editor indent your code. Use your editor's format command: in VS Code it's Format Document (Shift+Alt+F on Windows, Shift+Option+F on a Mac, Ctrl+Shift+I on Linux), in IntelliJ IDEA Reformat Code (Ctrl+Alt+L, or Cmd+Option+L on a Mac). After formatting, every block is indented one level deeper than the one around it, and a missing or extra brace shows up as code that suddenly sits too far left or right.
  2. Click next to a brace. Most editors highlight the matching brace. If the { of your if matches the } you meant for the method, you've found the problem.
  3. Type the closing brace straight away. When you write {, write the } right after it and then fill in the middle. Many editors do this for you.
  4. Fix the first error first. One wrong brace can cause several errors further down. Fix it, compile again, and many of the others disappear. Our guide How to read an error message explains why.

A quick checklist

  1. reached end of file while parsing: a { is missing its }. Look for an unclosed if, loop or method, usually somewhere above the line in the message.
  2. class, interface, enum, or record expected (and its Java 21 and 25 versions about unnamed classes, methods or fields): a } closed the class too early, or a method sits below the class.
  3. compact source file does not have main method in Java 25: almost always a method outside your class, not a problem with main itself.
  4. <identifier> expected on a statement: it's in the class but outside any method.
  5. illegal start of expression on a method: the method before it isn't closed.

Once your braces are right, the next error most beginners meet is about types: incompatible types: String cannot be converted to int. And when the program compiles but crashes while running, see Java NullPointerException.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Java

bad operand types for binary operator (Java fix)

Fix Java's bad operand types for binary operator '&&', '<', '>=' or '*': what first type and second type mean, the causes and fixes, with examples.

Error explained · · 13 min read

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right