Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!!");   } } 
like image 853
user241702 Avatar asked May 07 '14 23:05

user241702


People also ask

What does could not find or load main class mean?

When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.

Can't find or load main class error in NetBeans?

Sometimes due to out of memory space error, NetBeans is unable to load or find the main class. RightClick on the project node and go to Set configuration. Select the main class for your application. Then clean and build.

Could not find the main class program will exit?

There are two ways to do it: Reinstall the new JRE. It should then fix the file association in the OS. Fix the file association manually.


1 Answers

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  
like image 87
Kakarot Avatar answered Oct 04 '22 14:10

Kakarot