Javascript required
Skip to content Skip to sidebar Skip to footer

Error Could Not Find or Load Main Class Solution Hackerrank

How to fix "class, interface, or enum expected" error in Java? Example

If you have ever written Java programs using Notepad or inside DOS editor, then you know that how a single curly brace can blow your program and throw 100s of errors during compilation. I was one of those lucky people who started their programming on DOS editor, the blue window editor which allows you to write Java programs. I didn't know about PATH, CLASSPATH, JDK, JVM, or JRE at that point. It's our lab computer where everything is supposed to work as much as our instructor wants.

Since we don't have the internet at that point in time, we either wait for the instructor to come and rescue us and we were surprised by how he solve the error by just putting one curly brace and all errors mysteriously go away.

Today, I am going to tell you about one such error,"class, interface, or enum expected".   This is another compile-time error in Java that arises due to curly braces. Typically this error occurs when there is an additional curly brace at the end of the program.

Since everything is coded inside a class, interface, or enum in Java, once you close the curly brace for an existing class and add another closing curly braces, the compiler will expect another class is starting hence it will complain about class, interface, or enum keyword as shown in the following program:

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

If you compile this program using javac, you will get the following error:

$ javac Main.java Main.java:14:              class,              interface,              or              enum expected } ^ 1 error

Since this is a small program, you can easily spot the additional curly brace at the end of the problem but it's very difficult in a big program with several classes and methods.

How to fix "class, interface, or enum expected" error in Java? Example


This becomes even tougher if you are not using any IDE like Eclipse or Netbeans which will give you a visible sign of where an error is. If you know how to use Eclipse or any other Java IDE, just copy-paste your code into IDE and it will tell you the exact location of the error which hint to solve.

Alternatively, if you are coding in Notepad, I assume you are a beginner, then try to correctly indent your code. n our example program above, notice that the two curly braces at the end of the program are at the same indentation level, which cannot happen in a valid program. Therefore, simply delete one of the curly braces for the code to compile, the error will go away as shown below:

$ javac Main.java

So, next time you get the "class, interface, or enum expected" error, just check if you additional curly braces a the end of your program.

Other Java error troubleshooting experiences:

  • How to fix "variable might not have been initialized" error in Java? (solution)
  • Could not create the Java virtual machine Invalid maximum heap size: -Xmx (solution)
  • Error: could not open 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
  • How to fix java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory (solution)
  • Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger in Java (solution)
  • java.lang.OutOfMemoryError: Java heap space : Cause and Solution (steps)

Error Could Not Find or Load Main Class Solution Hackerrank

Source: https://www.java67.com/2016/08/how-to-fix-class-interface-or-enum-expected-error-java.html