HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java Examples / [Solved] Error: Could not find or load main class

[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 !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces