• Register
1 vote
26 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

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 259 views
259 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 Raphael Pacheco 4.9k points
0 votes
1 answer 1.2K views
1.2K 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 jwilliam 3.9k points
0 votes
1 answer 783 views
783 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 jwilliam 3.9k points
0 votes
2 answers 206 views
206 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 83 views
83 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