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
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 });
}
}
answered Feb 8, 2011 at 14:48
BigbohneBigbohne
1,3563 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
aioobeaioobe
411k112 gold badges807 silver badges825 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
5,0428 gold badges48 silver badges70 bronze badges
answered Apr 21, 2015 at 23:29
ntthusharantthushara
3213 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
My question is very simple. I am just wondering what the reached end of file while parsing error actually means / is. Why this error actually occurs. In addition, how I could avoid it. Before you say that this is a duplication of question it really isn’t because most questions I looked at are just examples with code. That is not what I am looking for I am just looking for a clear cut answer of why this error happens what it is / means, and how to avoid it that is it.
NOTE: I don’t understand «reached the end of the file». I know what parsing is, but I don’t know what it means by reached the end of file: while parsing. I also just want to now how to avoid this error, and why it occurs not just to understand it.
halfer
19.8k17 gold badges98 silver badges185 bronze badges
asked Mar 27, 2017 at 22:42
8
You could avoid this error by fixing the syntax in the code causing the parser to expect additional closing syntax indicators (like a right curly-brace). Also check the responses to the duplicate questions because they clearly explain what are generally the syntactical causes of same error.
answered Mar 27, 2017 at 22:51
2
package com.javarush.task.task04.task0408;
/*
Хорошо или плохо?
*/
public class Solution {
public static void main(String[] args) {
compare(3);
compare(6);
compare(5);
}
public static void compare(int a) {
...
}
ошибка:
com/javarush/task/task04/task0408/Solution.java:23: error: reached end of file while parsing
}
^
reached end of file while parsing:
Solution.java, line: 23, column: 2
reached end of the file while parsing
— Missing Class Curly Brace in Javareached end of the file while parsing
— Missingif
Curly Block Brace in Javareached end of the file while parsing
— Missing Loop Curly Brace in Javareached end of the file while parsing
— Missing Method Curly Brace in Java- Avoiding the
reached 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.
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.
This is a compile error, easily fixed
Updated on January 28, 2019
The Java error message Reached End of File While Parsing results if a closing curly bracket for a block of code (e.g, method, class) is missing.
The fix is easy — just proofread your code.
Example
In the code below, the method called main does not conclude with a closing curly bracket. The compiler cannot parse the code properly and will throw the error.
public class Main {
public static void main(String[] args) {
System.out.println("Oops missed a curly bracket..");
}
Avoiding the Error
Because this error is both common and easily avoided, using a code editor like Visual Studio Code or an integrated development environment like Eclipse. Code editors that feature linters and syntax checkers for Java (or whatever language you use) will uncover errors before you attempt to compile.
In this post, we will see how to solve reached end of file while parsing in java.
We generally get this error when we forgot to end code of block(class/method) with }
.
Let’s understand this with the help of example.
package org.arpit.java2blog; public class HelloWorld { public static void main(String[] args) { System.out.println(«Hello world from java2blog»); } |
When you compile above java file, you will get below error.
java:8: reached end of file while parsing }
Solution
To solve this issue, you just need to read code again and put curly brace at correct places.
For example:
In above program, we did not end main method correctly.
package org.arpit.java2blog; public class HelloWorld { public static void main(String[] args) { System.out.println(«Hello world from java2blog»); } } |
That’s all about how to solve reached end of file while parsing in java.