Main java 45 error reached end of file while parsing

This article introduces the reach end of file while parsing error in Java.
  1. reached end of the file while parsing — Missing Class Curly Brace in Java
  2. reached end of the file while parsing — Missing if Curly Block Brace in Java
  3. reached end of the file while parsing — Missing Loop Curly Brace in Java
  4. reached end of the file while parsing — Missing Method Curly Brace in Java
  5. Avoiding the reached end of file while parsing Error in Java

Fix the Reach End of File While Parsing Error in Java

This tutorial introduces an error reach end of the file while parsing during code compilation in Java.

The reached end of the file while parsing error is a compile-time error. When a curly brace is missing for a code block or an extra curly brace is in the code.

This tutorial will look at different examples of how this error occurs and how to resolve it. The reached end of file while parsing error is the compiler’s way of telling it has reached the end of the file but not finding its end.

In Java, every opening curly place ({) needs a closing brace (}). If we don’t put a curly brace where it is required, our code will not work properly, and we will get an error.

reached end of the file while parsing — Missing Class Curly Brace in Java

We missed adding closing curly braces for the class in the example below.

When we compile this code, it returns an error to the console. The reached end of file while parsing error occurs if the number of curly braces is less than the required amount.

Look at the code below:

public class MyClass {
    public static void main(String args[]) {
  
      print_something();
    } 

Output:

MyClass.java:6: error: reached end of file while parsing
    }
     ^
1 error

The closing brace of the MyClass is missing in the above code. We can solve this issue by adding one more curly brace at the end of the code.

Look at the modified code below:

public class MyClass {
   static void print_something(){
     System.out.println("hello world");
   }
    public static void main(String args[]) {
  
      print_something();
    }  
  } 

Output:

Let us look at the examples where this error can occur.

reached end of the file while parsing — Missing if Curly Block Brace in Java

The if block is missing the closing curly brace in the code below. This leads to the reached end of the file while parsing error during code compilation in Java.

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      if( x > 90){
        // do something
          System.out.println("Greater than 90");
    }  
}

Output:

MyClass.java:8: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by adding the curly brace at the appropriate place (at the end of the if block). Look at the code below:

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      if( x > 90){
        // do something
          System.out.println("Greater than 90");
      } // this brace was missing
    }  
}

The above code compiles without giving any error.

Output:

reached end of the file while parsing — Missing Loop Curly Brace in Java

The missing curly braces can be from a while or a for loop. In the code below, the while loop block is missing the required closing curly brace, leading to a compilation failure.

See the example below.

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      while( x > 90){
        // do something
        System.out.println("Greater than 90");
        x--;
      
    }  
}

Output:

MyClass.java:10: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by putting the curly brace at the required position (at the end of the while loop). Look at the modified code below:

public class MyClass {
    public static void main(String args[]) {
      int x = 38;
      while( x > 90){
        // do something
        System.out.println("Greater than 90");
        x--;
      } // This brace was missing
    }  
}

The above code compiles without giving any error.

Output:

reached end of the file while parsing — Missing Method Curly Brace in Java

In this case, we have defined a method whose closing brace is missing, and if we compile this code, we get a compiler error. Look at the code below.

public class MyClass {
    
    public static void main(String args[]) {
      customFunction();
    }  
    static void customFunction(){
      System.out.println("Inside the function");
    
}

Output:

MyClass.java:9: error: reached end of file while parsing
}
 ^
1 error

We can resolve this error by putting the curly brace at the required position (at the end of the function body). Look at the modified code below:

public class MyClass {
    
    public static void main(String args[]) {
      customFunction();
    }  
    static void customFunction(){
      System.out.println("Inside the function");
    }
}

Output:

Avoiding the reached end of file while parsing Error in Java

This error is very common and very easy to avoid.

To avoid this error, we should properly indent our code. This will enable us to locate the missing closing curly brace easily.

We can also use code editors to automatically format our code and match each opening brace with its closing brace. This will help us in finding where the closing brace is missing.

«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.

I have the following source code

public class mod_MyMod extends BaseMod
public String Version()
{
     return "1.2_02";
}
public void AddRecipes(CraftingManager recipes)
{
   recipes.addRecipe(new ItemStack(Item.diamond), new Object[] {
      "#", Character.valueOf('#'), Block.dirt
   });
}

When I try to compile it I get the following error:

java:11: reached end of file while parsing }

What am I doing wrong? Any help appreciated.

asked Feb 8, 2011 at 14:44

adeo8's user avatar

1

You have to open and close your class with { ... } like:

public class mod_MyMod extends BaseMod
{
  public String Version()
  {
    return "1.2_02";
  }

  public void AddRecipes(CraftingManager recipes)
  {
     recipes.addRecipe(new ItemStack(Item.diamond), new Object[] {
        "#", Character.valueOf('#'), Block.dirt });
  }
}

Andrew Tobilko's user avatar

answered Feb 8, 2011 at 14:48

Bigbohne's user avatar

BigbohneBigbohne

1,3463 gold badges12 silver badges24 bronze badges

1

You need to enclose your class in { and }. A few extra pointers: According to the Java coding conventions, you should

  • Put your { on the same line as the method declaration:
  • Name your classes using CamelCase (with initial capital letter)
  • Name your methods using camelCase (with small initial letter)

Here’s how I would write it:

public class ModMyMod extends BaseMod {

    public String version() {
         return "1.2_02";
    }

    public void addRecipes(CraftingManager recipes) {
       recipes.addRecipe(new ItemStack(Item.diamond), new Object[] {
          "#", Character.valueOf('#'), Block.dirt
       });
    }
}

answered Feb 8, 2011 at 14:49

aioobe's user avatar

aioobeaioobe

408k111 gold badges805 silver badges824 bronze badges

0

It happens when you don’t properly close the code block:

if (condition){
  // your code goes here*
  { // This doesn't close the code block

Correct way:

if (condition){
  // your code goes here
} // Close the code block

eebbesen's user avatar

eebbesen

5,0228 gold badges49 silver badges70 bronze badges

answered Apr 21, 2015 at 23:29

ntthushara's user avatar

ntthusharantthushara

3113 silver badges5 bronze badges

1

Yes. You were missing a ‘{‘ under the public class line. And then one at the end of your code to close it.

answered Aug 14, 2017 at 1:28

Techno Savage's user avatar

REACHED END OF FILE WHILE PARSINGERROR MESSAGE: REACHED END OF FILE WHILE PARSING

This is a common java compile error and by compile error I mean any type of error that prevent the compiler to translate adequately a java program.

Into its corresponding computer readable machine code that can be executed correctly.

Generally caused by incorrect syntaxes, a class not found, a bad class name or a bad method name and so on.  Analyzing the statement of the error message.

What the compiler meant by the word ‘parsing’ is that, during the process of syntactic analysis of the java program. A useful tool here would be a log parser, like this one that can be found here

The provided java program does not comply with the standard java program structure in terms of its syntactic composition.

In other words, the compiler is trying to say that it has reached the end of file without any evidence that the file has ended.

The file in this context does not in all cases mean a large java program, it could be just a block of java code but with a bad syntactic composition somewhere.

Note that this error message could also occur in C programming, but am not going to be discussing about C programming here since the error occur mostly in java programming.

But it is worth knowing that this error occurs in C programming when you are reading a file into a memory buffer (i.e. a physical memory storage used to temporarily store data.

While it is being moved from one place to another) and the number of bytes in your buffer is more than the number of bytes.

In the whole file or the size (in bytes) of the current read position to the end of the file. you will get this error or something related to it.

Follow me as I make a detailed explanation of the cause of this java error and the step by step approaches to take in order to fix the error.

Possible Causes of “Reached End of File while Parsing Error”

The general name for the cause of ‘reached end of file while parsing’ error is called Syntax Error.

The major syntax error occurs when a closing curly bracket for a block of code (e.g. private class, public class, method, if statement, switch statement, instance initialization blocks, etc.) is missing.

In some occasions, another bad syntax that could cause end of file.

While parsing error is a bad naming convention in which the name given to your class is not the same as the name you used to save your file (which ought to be the same).

The names of your methods do not comply with the standard java naming conventions.

Possible Solutions to “Reached End of File while Parsing Error”

The major fix or solution to this error message is to append a closing or ending curly brace to a corresponding opened curly braces.

which I will describe how to figure it out in the guides on how to fix the error section. Another solution is to make sure your java program complies with the standard Java coding naming conventions.

And note that java is a case sensitive programming language, thus, apart from the name of your file and the name of the public class being the same, the case also should the same.

Guides on how to fix “Reached End of File while Parsing Error”

You can carefully go through your codes and figure out where you have opened a curly brace but failed to provide an ending brace for such opened brace.

However, this approach could be time consuming in case of large java programs, therefore, you can first add a curly brace at the end of your program to do a random check.

The later approach described is also inefficient in cases whereby the omitted brace is an open brace or maybe the problem is that you have mistakenly added a redundant opening or closing brace.

Therefore, the best approach is still to carefully read through the java program and figure out where there is or are mismatch in curly braces.

You can also quickly discover this error by counting the number of open braces in your program and determining whether it tally with the number of closing braces.

If you are using IDE’s like NetBeans and Eclipse, an easier way to diagnose where the error is in the code, is to use a combination of the key “Alt + Shift + F”.

This will cause the IDE to automatically indent the codes properly and align matching braces with their corresponding control structures which will make it easier for you to see where the missing brace is located.

For the other cause of the error i.e. incorrect class and method naming, it could be fixed by going through your program and using the standard java coding conventions where necessary.

A few of which include putting the open brace on the same line with the method or class declaration, naming your classes and methods.

Using camel case; a practice of writing phrases in which each word in the middle of the phrase begins with a capital letter with no intervening spaces or punctuation.

How to avoid “Reached End of File while Parsing Error”

This error can be avoided by using software called Integrated Development Environment (IDE) such as Eclipse and NetBeans to write your Java programs.

These softwares have inbuilt syntax checkers that can automatically inform you when a block of code does not have a closing brace.

Some of these IDE’s will even automatically append a closing brace to every brace you open just in a bit to avoid these reached end of file error.

Intelligent code editors like Visual Studio code and Sublime Text can also be used to write your java programs as they are also capable of uncovering errors in your programs before you attempt to compile them.

Another way to avoid the error caused by the error message “reached end of file while parsing error” is to adopt a clean coding structure.

By proper indentations and clear separation of individual classes and methods to make it easier for you to locate any unclosed brace.

For more fixes on common Java Errors, check out our articles below:

  • Java error code 1618 Java Script Update Error [solved]
  • How To Fix Discord Javascript Error [Fatal Error]
  • How to fix Java Code 1603
  • Javascript Void (0) Fix
  • Javascript Try Catch Not Working
  • Javascript Uncaught TypeError
  • Javascript Error Uncaught Exception Fix

Понравилась статья? Поделить с друзьями:
  • Main init error перевод
  • Main init error system exception inventory manager standoff 2
  • Make stop error code 1
  • Make oldconfig error 127
  • Make no targets specified and no makefile found stop ошибка