[Solved] Error: Reached end of file while parsing

"reached end of file while parsing" is a java compiler error. This error is mostly faced by java beginners. You can get rid of such kind of errors by coding simple java projects. Let's dive deep into the topic by understanding the reason for the error first.

Read Also: Missing return statement in Java error

1. Reason For Error

This error occurs if a closing curly bracket i.e "}"  is missing for a block of code (e.g method, class).

Note: Missing opening curly bracket "{" does not result in the "reached end of file while parsing" error. It will give a different compilation error.

[Fixed] Reached end of file while parsing error in java

1. Suppose we have a simple java class named HelloWorld.java

public class HelloWorld
{
    public static void main(String args[])
    {
       System.out.println("This class is missing a closing curly bracket");
    }

If you try to compile the HelloWorld program using below command

javac HelloWorld.java

Then you will get the reached end of file while parsing error.

reached end of file while parsing java error


If you look into the HelloWorld program then you will find there is closing curly bracket "}" missing at the end of the code. 

Solution: just add the missing closing curly bracket at the end of the code as shown below.

public class HelloWorld
{
    public static void main(String args[])
    {
       System.out.println("This class is missing a closing curly bracket");
    }
}

2.  More Examples 

2.1  In the below example main() method is missing a closing curly bracket.

public class HelloWorld
{
    public static void main(String args[])
    {
       System.out.println("Main method is missing a closing curly bracket");
    
}

2.2 In the below example if block is missing a closing curly bracket.

public class HelloWorld
{
    public static void main(String args[])
    {
       if( 2 > 0 )
       {
            System.out.println("if block is missing a closing curly bracket");
    }
}

Similarly, we can produce the same error using while, do-while loops, for loops and switch statements.

I have shared 2.1 and 2.2 examples that you should fix by yourself in order to understand the reached end of file while parsing error in java.

3. How to Avoid This Error

You can avoid this error using the ALT + Shift + F command in Eclipse and Netbeans editor. This command autoformats your code then it will be easier for you to find the missing closing curly bracket in the code.

That's all for today. If you have any questions then please let me know in the comments section.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry