Thursday, July 31, 2008

A Problem When Running a Java Program

I had recently installed Java 6. And it compiled the source file fine. Then when I tried to run the compiled file it gave the following error.

D:\Java\Examples>javac HelloWorld.java

D:\Java\Examples>java HelloWorld
Exception in thread "main" java.lang.NoClassDe fFoundError: HelloWorld
Caused by: java.lang.ClassNotF oundException: HelloWorld
at java.net.URLClassLo ader$1.run( Unknown Source)
at java.security. AccessController .doPrivileged( Native Method)
at java.net.URLClassLo ader.findClass( Unknown Source)
at java.lang.ClassLoad er.loadClass( Unknown Source)
at sun.misc.Launcher$ AppClassLoader. loadClass( Unknown Source)
at java.lang.ClassLoad er.loadClass( Unknown Source)
at java.lang.ClassLoad er.loadClassInte rnal(Unknown Source)

Now I was wondering what went wrong? I mean, I know that it compiled fine. But it refused to run. So what to do? I tried compiling and running a different source file. Nope the answer is the same. And then I compiled and tried to run a source file that I was really sure that was error free which happens to be HelloWorld.java. The same error occured.

What is the point of being a programmer if you can't run HelloWorld in java?

Then I thought I would need some expert advice on the matter. So I posted the matter to a lecturer at the institute where I study for BIT. This is the reply that I got.

To immediately solve this problem, you can delete the "classpath" variable in Environment variables list. Try in this manner, System properties--> Advanced --> Environment Variables --> Select "classpath" and Delete it.

So I did exactly that and all the java programs are running fine since then.