Resolve Error: Could not find or load main class

You are here : Home / Core Java Tutorials /

Contents of page >
  • Solving solve Error: Could not find or load main class >
  • Why Error: Could not find or load main class occurred?
  • Another scenario when Error: Could not find or load main class may occur >



Solving solve Error: Could not find or load main class >

In this post we will solve Error: Could not find or load main class

First create below java program >
package myPackage;
public class MyClass {
public static void main(String[] args) {
     System.out.println("This is MyClass");
}
}

When you execute above program as java application in eclipse output will be >
This is MyClass


Let’s say above java file is in directory=  E:\workspace\MyProject\src\myPackage\MyClass.java


When you execute above program as java application in eclipse class file is formed, but when you haven’t executed above program we can create .class file by cleaning and building eclipse project.

Let’s clean and build the eclipse project >

When project is clean and builded using eclipse, .class file will be formed in directory=  E:\workspace\MyProject\bin\myPackage\MyClass.class

Let’s try to execute above program from CMD,  Go to CMD, go to .class file directory and type below command >

E:\workspace\MyProject\bin\myPackage> java MyClass
Error: Could not find or load main class MyClass

E:\workspace\MyProject\bin>

But, Error: Could not find or load main class MyClass was produced.

For solving this Error, Go one directory above using cd..

E:\workspace\MyProject\bin\myPackage> cd..

E:\workspace\MyProject\bin> java myPackage.MyClass
This is MyClass

E:\workspace\MyProject\bin>


And problem is resolved, class file is executed successfully!



Why Error: Could not find or load main class occurred?
JVM tries to search for class in E:\workspace\MyProject\bin with name myPackage.MyClass

If class wouldn't have been in myPackage then this problem wouldn’t have occurred.
But when class is in some package you need to pass class name prefixed with package name and .
Example - rather than using MyClass, go one directory up and use myPackage.MyClass



Another scenario when Error: Could not find or load main class may occur >
Using java myPackage.MyClass.class
in place of  java myPackage.MyClass

can cause Error: Could not find or load main class.

We just need to remove additional .class included with java command.
E:\workspace\MyProject\bin\myPackage> cd..

E:\workspace\MyProject\bin> java myPackage.MyClass.class
Error: Could not find or load main class myPackage.MyClass.class

E:\workspace\MyProject\bin>


Summary >
In this core java tutorial we solved Error: Could not find or load main class, Why Error: Could not find or load main class occurred? scenarios when Error: Could not find or load main class may occur.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Find out Java version (32 or 64 bit) installed in your system using java program and cmd


Find out JDK version installed in your system using java program


Find eclipse version in your system


Labels: Core Java
eEdit
Must read for you :