Click to See Complete Forum and Search --> : java.lang.NoClassDefFoundError ?


mahmood
10-09-2005, 09:11 AM
Hi

I was able to compile and run java files until a few day ago. Yesterday when I tried to run a file I got: "java.lang.NoClassDefFoundError: TestClass Exception in thread main "

I am sure that PATH is ok because I havent changed anything there. NetBeans works fine but I don't want to work with NetBeans because it adds a lot of folders and things that I dont like and I am doing thing for learing.

I reinstalled java but to no good.

Any suggestion?

Norm
10-09-2005, 11:46 AM
Sounds like your classpath doesn't contain a path to TestClass.
Can you show/paste here the commandline you are entering and the results?
Also if you are using the CLASSPATH environ variable, show its value.

The PATH env var is NOT related to the CLASSPATH var. PATH tells the OS where to find commands. CLASSPATH is used by Java to find class definitions.

What's changed? If it used to work and doesn't now, what did you change? Can you change it back?

mahmood
10-09-2005, 01:38 PM
Thanks Norm for reply

I edit this topic to add this:
I noticed you said: "CLASSPATH is used by Java to find class definitions." so I changed CLASSPATH to the directory that I had my class and it worked. Thanks but do I need to change it for every different directory that I have my java files there?

and the rest is what I typed before reaching the above result.

Actually since the problem started I changed and changed PATH and CLASSPATH to whatever websites suggested but no hope. This is what I have now:

PATH: D:\programs\Java\jdk1.5.0_04\bin
CLASSPATH: D:\programs\Java\jre1.5.0_04\lib - I tried D:\programs\Java\jre1.5.0_04\lib\ext and D:\programs\Java\jdk1.5.0_04\bin as well. -

and my test program:

import java.lang.*;
class TestClass{
public static void main(String[] args){

}
}

It compiles ok but when I try to run it I get this error:


Exception in thread "main" java.lang.NoClassDefFoundError: TestClass


And I add that I can run the code using NetBeans.

Norm
10-09-2005, 04:29 PM
You shouldn't have to set the CLASSPATH to the java SDK files. You should NOT put any of your programs in the java folders.

I don't see the commandline you entered as per earlier request:

>>>Can you show/paste here the commandline you are entering and the results?

What folder are you in when you issure the command? What files are in that folder?

mahmood
10-10-2005, 04:13 PM
Thanks for reply

I noticed you said: "CLASSPATH is used by Java to find class definitions." so I changed CLASSPATH to the directory that I had my class and it worked. Thanks but do I need to change it for every different directory that I have my java class files are there?

Norm
10-10-2005, 06:19 PM
Depends. All the classes to be used in an execution need to be found by java. So if a class is needed, then it should be on the classpath.
A prefered method is to set the classpath with the java.exe command when you execute the program:

java.exe -classpath <path>;. <pgm>

kalyson
10-14-2005, 05:45 PM
"I noticed you said: "CLASSPATH is used by Java to find class definitions." so I changed CLASSPATH to the directory that I had my class and it worked. Thanks but do I need to change it for every different directory that I have my java class files are there?"


If I were you, to avoid that particular inconvenience I'd add the current directory to my classpath so that any programs in the current directory would be found. In Unix, I add "./" to the classpath, but in windows I imagine it would be ".\" instead.

jejik
10-20-2005, 05:02 AM
I had the same problem. All of a sudden (it felt) my java-progs stopped working with NoClassDefFoundError thrown, even hello world progs.

The reason turned out to be that a program I installed (XML SPy) had added CLASSPATH as env variable. Now i had CLASSPATH=c:\XMLSpy. If I changed that to CLASSPATH=.;c:\XMLSpy my java-progs started working.
It also works if I unset my CLASSPATH.

gulapala
10-20-2005, 07:42 AM
set the classpath to you current directory(.) either in your environement variables or set teh classpath explicitly to you current directory. It will Work

thanks
G.Rajasekhar