[java] Could not find main class HelloWorld

I installed Java 1.7.0 in the following folder C:\Program Files\Java. My operating system is Windows XP(Version 2002) with Service pack 3.

The environment variables which I set are:

  • CLASSPATH : C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jar;

  • Path : C:\Program Files\Java\jdk1.7.0\bin;

  • JAVA_HOME : C:\Program Files\Java;

I have presented here the class names which are in my system.

Next I wrote a program, HelloWorld.java:

import java.io.*;

class HelloWorld
{
    public static void main(String[] args) 
    {
        System.out.println("Hello World!");
    }
}

When I am compiling using javac HelloWorld.java it is compiling fine.

But after I issue java HelloWorld I am encountering the below error:

Error: Could not find main class HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:198)

Caused by: java.lang.ClassNotFoundException: HelloWorld
        at java.net.URLClassLoader$1.run(URLClassLoader.java:299)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:288)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:287)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:422)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:355)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:195)

After a bit of searching around, I found that may be something wrong in the environment variable. I tried to play with that but no luck.

I even RESTARTED the machine and then again I tried to run but with same fate.

This question is related to java

The answer is


Just remove your "classpath" from you environment variable. Then try running:

java HelloWorld 

This should work fine.


You either want to add "." to your CLASSPATH to specify the current directory, or add it manually at run time the way unbeli suggested.


JAVA_HOME is not necessary if you start java and javac from the command line. But JAVA_HOME should point to the real jdk directory, C:\Program Files\Java\jdk1.7.0 in your case.

I'd never use the CLASSPATH environment variable outside of build scripts, especially not global defined. The -cp flag is better. But in your case, as you do not need additional libraries (rt.jardoesn't count), you won't need a classpath declaration. A missing -cp is equivalent to a -cp . and that's what you need here)

The HelloWorld class needs to be declared as public. This actually may be the cause for your problems. (I was pretty sure, that a source file needs one public class... or was it one public class at most ?)


It looks that you had done all setup properly but there might be one area where it might be causing problem

Check the value of your "CLASSPATH" variable and make sure at the end you kept ;.

Note: ; is for end separator . is for including existing path at the end


Java is not finding where your compiled class file (HelloWorld.class) is. It uses the directories and JAR-files in the CLASSPATH environment variable for searching if no -cp or -classpath option is given when running java.exe.

You don't need the rt.jar in the CLASSPATH, these was only needed for older versions of Java. You can leave it undefined and the current working directory will be used, or just add . (a single point), separated by ';', to the CLASSPATH variable to indicate the current directory:

CLASSPATH: .;C:\...\some.jar

Alternatively you can use the -cp or -classpath option:

java -cp . HelloWorld

And, as Andreas wrote, JAVA_HOME is not needed by Java, just for some third-party tools like ant (but should point to the correct location).


I have also faced same problem....

Actually this problem is raised due to the fact that your program .class files are not saved in that directory. Remove your CLASSPATH from your environment variable (you do no need to set classpath for simple Java programs) and reopen cmd prompt, then compile and execute.

If you observe carefully your .class file will save in the same location. (I am not an expert, I am also basic programer if there is any mistake in my sentences please ignore it :-))


I had the same problem. Perhaps, the problem is that you have compiled and executed the class with different Java versions.

Make sure the version of the compiler is the same as the command "java":

javac -version

java -version

In Linux, use

sudo update-alternatives --config java

to change the version of Java.


Tell it where to look for you class: it's in ".", which is the current directory:

java -classpath . HelloWorld

No need to set JAVA_HOME or CLASSPATH in this case


put .; at classpath value in beginning..it will start working...it happens because it searches the class file in classpath which is mentioned in path variable.