• Register
1 vote
37 views

Problem:

What is the meaning of Java could not find or load main class,  and why does it happen?

I’ll appreciate your sharing.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

This error can occur for a couple of reasons. Let me walk through every possible solution step by step.

1. Calling  .class file from java command:

public class HelloWorld {

    public static void main(String args[]) {
        System.out.println(" Hello World !");       
    }
}

Let's assume I have the basic program above. If I will compile it using the command, HelloWorld.class will be created. 

javac HelloWorld.java

Now I want to run the .class file. I will get the error message if I run

java HelloWorld.class

Instead of

java HelloWorld

 

2. Incorrect Casing: We all know that Java is a case sensitive language. We need to keep in mind whenever we declare a variable, function or even class name.

So, we’ll get the error message if we run

 

java helloworld

instead of

java HelloWorld

 

3. Class in a package

Have a close look at the program below.

package com.naymulhasan;
/**
 * Java program to demonstrate 
 * Error :Could not find or load main class
 * 
 * @author Naymul Hasan
 */
public class HelloWorld {

    public static void main(String args[]) {
        System.out.println(" Hello World !");       
    }
}

Here I have HelloWorld class inside com.naymulhasan package. In this case, if I try to call

java HelloWorld

it will give the same error message. Because I had to call the class file with the package name. Let me try this one

java com.naymulhasan.HelloWorld

Oops! I am still getting the error message because the CLASSPATH environment variable is not set. Let’s try with the CLASSPATH command -cp

java -cp . com.naymulhasan.HelloWorld

BOOM. You are all set now.

13 9 6
94,260 points

Related questions

0 votes
1 answer 287 views
287 views
I wrote a java code that gives some error: Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException at org.hibernate.boot.spi.XmlMappingBinderAccess.<init>(XmlMappingBinderAccess.java:43) at org.hibernate.boot.MetadataSources.< ... .initializeBean(AbstractAutowireCapableBeanFactory.java:1700) ... 25 common frames omitted Can anyone help me out with this?
asked Mar 12, 2020 LizzyM 6.1k points
1 vote
1 answer 3 views
3 views
Problem: As a new Java developer I would like to know the reason of this error? Error: could not find or load main class helloworld.java.
asked Apr 26 PkGuy 23.5k points
0 votes
1 answer 15 views
15 views
Problem: Any ideas on what I need to fix the error: could not find or load main class main caused by: java.lang.classnotfoundexception: main?
asked Mar 27 ashik 14.6k points
0 votes
1 answer 4 views
4 views
Problem: What is the meaning of Java could not find or load main class, and why does it happen?
asked Mar 27 ashik 14.6k points
0 votes
1 answer 11 views
11 views
Problem: I used Google to try to find a solution on the web of the above question, But I got no useful hints. Can I get Pointer information about my problem with this?
asked Mar 21 rubelkhan 20.4k points
0 votes
1 answer 19 views
19 views
Problem: java jar could not find or load main classI
asked Feb 24 Hira 2.2k points
0 votes
1 answer 16 views
16 views
What does this mean, what causes it, and how should you fix it?
asked Dec 15, 2020 TeamScript 13.5k points
0 votes
1 answer 15 views
15 views
I have a Java project I am working on and I get this error. Please solve this.
asked Dec 3, 2020 TeamScript 13.5k points
0 votes
1 answer 62 views