Main Tutorials

Java – Could not find or load main class

A popular error message for new Java users.


Error: Could not find or load main class ClassName.class
Caused by: java.lang.ClassNotFoundException: ClassName.class

1. No Package

1.1 Reviews a simple Java Hello World, no package.

C:\projects\HelloWorld.java

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World");
		
    }
	
}
Terminal

# Compile Java source code
C:\projects> javac HelloWorld.java

C:\projects> dir
15/04/2019  01:20 PM               425 HelloWorld.class
15/04/2019  01:42 PM               138 HelloWorld.java

# Run Java
C:\projects>java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class

To fix it, run java ClassName, without .class extension.

Terminal

C:\projects>java HelloWorld
Hello World

2. With Package

2.1 A simple Hello World and package it as com.mkyong

C:\projects\HelloWorld.java

package com.mkyong;

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World");
		
    }
	
}
Terminal

# Compile Java source code
C:\projects> javac HelloWorld.java

C:\projects> dir
15/04/2019  01:20 PM               425 HelloWorld.class
15/04/2019  01:42 PM               138 HelloWorld.java

C:\projects>java HelloWorld
Error: Could not find or load main class HelloWorld
Caused by: java.lang.NoClassDefFoundError: com/mkyong/HelloWorld (wrong name: HelloWorld)

2.2 If package is defined, the Java classes .class must place in the correct folder location.

For example :

C:\projects\HelloWorld.java

package com.mkyong;

The above generated class file must place in C:\projects\com\mkyong\HelloWorld.class

2.3 To fix it, we can use -d to specify where to place generated class files.

Terminal

# Compile Java source code
C:\projects> javac -d . HelloWorld.java

C:\projects> dir

15/04/2019  02:01 PM    <DIR>          com
15/04/2019  01:51 PM               161 HelloWorld.java

C:\projects\com\mkyong\HelloWorld.class
C:\projects\HelloWorld.java

2.4 Now, we can run like this java {package}.HelloWorld

Terminal

C:\projects>java HelloWorld
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

C:\projects>java com.mkyong.HelloWorld
Hello World

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
baser
3 years ago

Thank you. I search too many site for this error. Your solution is ok.

M Amin
4 years ago

Many thanks for your Help, but in my case I was seeing the error line only and not Caused by line
How did you make the Caused by line appears after the error line ?
thanks

zikyoubi
4 years ago

Thanks you are the best Mkyong ^^

Gaurav Pandey
2 years ago

Sharing same issue I faced in Eclipse. If you ever faced this then what should you Do?
Here is simple 3 steps which can solve this error.

  1. Open your Eclipse and Do right click on your Project.
  2. Go to Build Path > Configure Build Path
  3. Here you see Java Build Path (In case If you have Selected Java Project)
  4. Top of Menu you see Order and Export in Eclipse
  5. Error was coming because the 3 out of 2 Check boxes were unchecked

a) JRE Library
b) Selenium Server Standalone
After selection and Saving them it will RUN your program and Error will Resolved.

Bhagwandas
2 years ago

root@localhost:/LIST/Test1#java JTableDemo12

Error: Could not find or load main class JTableDemo12
Caused by:java.lang.NoClassDefFoundError: com/mykong/JTableDemo12 (wrong name: JTableDemo12)

root@localhost:/LIST/Test1# java com.mykong.JTableDemo12

Error: Could not find or load main class com.mykong.JTableDemo12 Caused by: java.lang.ClassNotFoundException: com.mykong.JTableDemo12

a d
3 years ago

Thank you so much. I looked at 17 other websites and could not figure it out but your solution worked.

Bai Ivan
3 years ago

Thank you man!