• Register
1 vote
28 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,240 points

Related questions

0 votes
1 answer 6 views
6 views
What does this mean, what causes it, and how should you fix it?
asked Dec 15, 2020 TeamScript 9.9k points
0 votes
1 answer 9 views
9 views
I have a Java project I am working on and I get this error. Please solve this.
asked Dec 3, 2020 TeamScript 9.9k points
0 votes
1 answer 3 views
3 views
What does this mean, what causes it, and how should you fix it?
asked 3 hours ago TeamScript 9.9k points
0 votes
1 answer 21 views
21 views
Solve this.
asked Dec 28, 2020 TeamScript 9.9k points
0 votes
1 answer 955 views
955 views
Problem: I am very new to Cucumber. Recently I was reworking my project in the Intellij into my modules and suddenly I came across below error: "C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-Dorg.jetbrains.run.directory=C:\Users\Raphael\Documents\1_Code Stuff ... guru-pet-clinic" Error: Could not find or load main class cucumber.cli.Main Can some Cucumber expert help me in fixing above error?
asked Jul 27, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 1.9K views
1.9K views
Problem : I cleaned my whole project by deleting my local directories as following ~/.gradle, ~/.m2 ~./android and ~/workspace/project/.gradle and choosing the File -> Invalidate Caches / Restart... in my Android Studio. Now execution of my command ./gradlew leads to the ... say that I deleted too much code, the question now is how can it be repaired again? Do you have any solution to fix this?
asked Jan 18, 2020 jwilliam 3.9k points
0 votes
1 answer 1.3K views
1.3K views
Problem : When I run the apache tomcat7.0.56 in Eclipse, I get below error Error: Could not find or load main class org.apache.catalina.startup.Bootstrap How can I fix the above problem?
asked Jan 18, 2020 jwilliam 3.9k points
0 votes
2 answers 257 views
257 views
Problem : I am stuck with following Maven issue. Error: could not find or load main class org.codehaus.plexus.classworlds.launcher.launcher My Maven was working fine till last week after that I do not know what went wrong with it now it gives me the error I have tried replacing ... Java(TM) SE Runtime Environment (build 1.7.0_75-b13) Java HotSpot(TM) 64-Bit Server VM (build 24.75-b04, mixed mode)
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 94 views
94 views
Problem : I am getting bellow compiler error error: could not find or load main class com.sun.tools.javac.main
asked Nov 5, 2019 peterlaw 6.9k points
0 votes
1 answer 173 views
173 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