"Could not find or load main class" Error while running java program using cmd prompt
"Could not find or load main class" Error while running java program using cmd prompt
Question
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!!");
}
}
Popular Answer
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
Read more... Read less...
What's your CLASSPATH
value?
It may look like this:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
I guess your value does not contain this .;
.
So, ADD IT .
When you done , restart CMD
That may works.
For example the file HelloWorld.java
is in path: D:\myjavatest\org\yz\test
and its package
is: org.yz.test
.
Now, you're in path D:\myjavatest\
on the CMD line.
Type this to compile it:
javac org/yz/test/HelloWorld.java
Then, type this to run it:
java org.yz.test.HelloWorld
You may get what you want.
I removed bin
from the CLASSPATH
. I found out that I was executing the java
command from the directory where the HelloWorld.java
is located, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld
So I moved back to the main directory and executed:
java org.tij.exercises.HelloWorld
and it worked, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld
Hello World!!
Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath
and -cp
are not used and CLASSPATH
is not set, the current directory is used (.
), however when running .class
files, you need to be in the folder which consist Java package name folders.
So having the .class
file in ./target/classes/com/foo/app/App.class
, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH
variable:
- Linux:
echo $CLASSPATH
- Windows:
echo %CLASSPATH%
which has entries separated by :
.
See also: How do I run Java .class files?
One reason for this error might be
Could not find or load main class
<class name>
Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
you can save as Hello.java
but,
To Compile : javac Hello.java
This will auto generate A.class file at same location.
Now To Run : java A
I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:
C:\Java Example>java -cp . HelloWorld
The -cp
option for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.
If you need more information about the java options enter java -?
or java -help
I think the options are not optional.
I just did some more research I found a website that goes into detail about CLASSPATH
. The CLASSPATH
must be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:
// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.
When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.