"Could not find or load main class" Error while running java program using cmd prompt

user241702 picture user241702 · May 8, 2014 · Viewed 313.5k times · Source

I am running a simple "HelloWorld" Program. I get this error in the command prompt:

Could not find or load main class HelloWorld.

I have set the CLASSPATH and PATH variable in the system. In the cmd prompt, I am running from the directory where I have saved HelloWorld program. I can see the class name and the file name are same and also .class file created in the same directory. What else could be the problem?

My sample program looks like this:

package org.tij.exercises;
public class HelloWorld {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello World!!");
  }
}

Answer

Kakarot picture Kakarot · May 8, 2014

When the Main class is inside a package then you need to run it as follows :

java <packageName>.<MainClassName>

In your case you should run the program as follows :

java org.tij.exercises.HelloWorld