[Solved] Error: Could not find or load main class

We may get this error (Could not find or load main class) while running the Java class (e.g. Hello world application) from the system console. We get this error because we are incorrectly trying to run the main() inside the class using java command.

1. Reason for error – could not find or load main class

Consider the below Java class which has a main() method. We are trying to execute this class using the command line.

package com.howtodoinjava.core.basic;

public class MainClass 
	public static void main(String[] args) 
		System.out.println("Hello, World!"); 

If we try to navigate to the location where the .java file has been created, and try to run the example, we will get this error.

E:\java-examples\src\com\howtodoinjava\core\basic> javac MainClass.java

E:\java-examples\src\com\howtodoinjava\core\basic> java MainClass
Error: Could not find or load main class MainClass

2. Solution

If we want to execute the main() method in MainClass, we must use the full class name, including package name, in the java command.

The correct way to compile and execute this class is :

E:\java-examples\src> javac com\howtodoinjava\core\basic\MainClass.java

E:\java-examples\src> java com.howtodoinjava.core.basic.MainClass

Program output

Hello, World!

Great. The application ran successfully. In this way, by using the correct class name with the package name, and executing the java command from the package root folder we can solve this error.

Happy Learning !!


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.