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.
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.
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.
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
You are not setting a classpath that includes your compiled class! java
can't find any classes if you don't tell it where to look.
java -cp [compiler outpur dir] HelloWorld
Incidentally you do not need to set CLASSPATH the way you have done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With