- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the reason behind the error “Could not found or load main class” in java?
When write a Java program/class first of all you need to compile it using the javac command as −
javac [name of the class].java
If your program gets compiled without errors, a .class file (byte code) is created with the specified name. Then you need to execute it using the java command (JVM) as −
java [class name]
Example
Suppose we Have created a simple class Calculator which adds two or, three numbers in the file with name Calculator.java in the path D:\sample as −
public class Calculator { int addition(int a , int b){ int result = a+b; return result; } int addition(int a , int b, int c){ int result = a+b+c; return result; } public static void main(String args[]){ Calculator obj = new Calculator(); System.out.println(obj.addition(12, 13)); System.out.println(obj.addition(12, 13, 15)); } }
Compilation
First of all, we should compile the Calculator.java using the javac command as −
D:\sample>javac Calculator.java
If this program executes without compilation errors a .class file with name Calculator.class will be generated in the current folder as −
Execution
Then, we can execute the generated byte code using the java command (JVM) as −
java Calculator
Output
25 40
“Could not found or load main class” error
While executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class” error as −
D:\sample>java Calculator Error: Could not find or load main class Calculator Caused by: java.lang.ClassNotFoundException: Calculator
To avoid this error, you need to specify the absolute (including packages) name of the .class file (just name) which is in the current directory.
If you see this error while executing a program −
- Wrong class name − You might have specified the wrong class name.
- Wrong case − You need to specify the name of the class with same case Example.java is different from example.java.
- Wrong package − You might have created the .class file in a package and tried to execute without package name or with wrong package name.
Example
package sample.example; class Calculator { int addition(int a , int b){ int result = a+b; return result; } public static void main(String args[]){ Calculator obj = new Calculator(); System.out.println(obj.addition(12, 13)); } }
Here we have specified a package name in the program and compiled it as −
D:\sample>javac -d . Calculator.java
While execute we need to specify the correct package name in which the .class file exists as −
D:\sample>java sample.example.Calculator 25
- Inclusion of .class extension − While executing a file there is no need to include the .class extension in your program you just need to specify the name of the class file.
D:\sample>java Calculator.class Error: Could not find or load main class Calculator.class Caused by: java.lang.ClassNotFoundException: Calculator.class
- Related Articles
- How to resolve "Could not find or load main class package" in Java?
- What is the reason behind heart attack?
- What is Xcode error “Could not find Developer Disk Image”?
- What is the reason behind feeding crows during Shraddhas?
- What is the scientific reason behind we getting goosebumps?
- While using SAP .NET connector, I am an getting error: Could not load file or assembly 'sapnco' or one of its dependencies.
- What are the differences between an Exception class and an Error class in Java?\n
- How to deal with “could not find function” error in R?
- What is the class "class" in Java?
- Load class with forName() method in Java
- What is the reason behind Naxalism in India and how can we get rid of it?
- What happens if NullPointerException is not handled in main method of java?
- What was the reason behind the success of India in Hockey once upon a time?
- What was the reason behind the success of Mohan Bagan once upon a time?
- Name the two main systems found in a plant.
