tutorialcup
Could not find or load main class in Java
In some situations, while executing a Java program from the command prompt, we may face the error "Could not find or load main class". This occurs mainly when the JVM fails to find the main class or .class file. Whenever we compile a Java code, the compiler will automatically create a .class file with the same name as the class name. This will be present in the same directory where we have the .java file.
Let us see a simple Java code and try to compile and execute it from the command prompt.
public class HelloJava {
public static void main(String args) {
System.out.println("Java programming language");
}
}
First, we need to compile the code using the javac command.
D:Sample>javac HelloJava.java
This will produce the right result as below since we have compiled it correctly.
D:Sample>java HelloJava
Java programming language
Now let us see the different reasons or causes to generate the error "Could not load or find the main class in Java".
Could not find or load main class in Java: Incorrect class name
Whenever we compile the java code, the compiler creates a .class file in the same name as the class name. After compilation, if we try to run the code by providing an incorrect file name in the java command, it will result in an error "Could not load or find the main class in Java".
D:Sample>java hellojava
Error: Could not find or load main class hellojava
Caused by: java.lang.NoClassDefFoundError:
www.tutorialcup.com/java/could-not-find-or-load-main-clas...
Could not find or load main class in Java
In some situations, while executing a Java program from the command prompt, we may face the error "Could not find or load main class". This occurs mainly when the JVM fails to find the main class or .class file. Whenever we compile a Java code, the compiler will automatically create a .class file with the same name as the class name. This will be present in the same directory where we have the .java file.
Let us see a simple Java code and try to compile and execute it from the command prompt.
public class HelloJava {
public static void main(String args) {
System.out.println("Java programming language");
}
}
First, we need to compile the code using the javac command.
D:Sample>javac HelloJava.java
This will produce the right result as below since we have compiled it correctly.
D:Sample>java HelloJava
Java programming language
Now let us see the different reasons or causes to generate the error "Could not load or find the main class in Java".
Could not find or load main class in Java: Incorrect class name
Whenever we compile the java code, the compiler creates a .class file in the same name as the class name. After compilation, if we try to run the code by providing an incorrect file name in the java command, it will result in an error "Could not load or find the main class in Java".
D:Sample>java hellojava
Error: Could not find or load main class hellojava
Caused by: java.lang.NoClassDefFoundError:
www.tutorialcup.com/java/could-not-find-or-load-main-clas...