Exception in thread "main" java.lang.NoClassDefFoundError

 

>> Register as FREELANCE DEVELOPER for FREE!


A friend of mine wanted to start to learn java, and I wanted to help him, so we created a simple Hello World example.

public class Hello {

public static void main(String argv[]) {

System.out.println("Hello world !");

}

}

then, when you compile it:

> javac Hello.java

and want to execute it:

> java Hello

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

We set the CLASSPATH and JAVA_HOME enviroment variable correctly, and the problem was still the same..

When we copied the Hello.class into a lib directory of JAVA_HOME, it worked, but not from the current directory !

We tried different versions of java.. and do you know where this stupid problem is?

in the CLASSPATH there should be:
CLASSPATH=.;c:\programme\jboss\lib;c:\programme\
j2sdk1.4.2_08\lib;c:\programme\j2sdk1.4.2_08\jre\lib; ...

yes.. a dot .. it means, that it will search for classes bytecode in the current directory !

you can also tell java to search the current directory with the -classpath parameter

> java -classpath . Hello

Problems like this can really discourage java newbies, and that moment was very embarassing :)




 

Problem:
Basically, the Exception in thread "main" java.lang.NoClassDefFoundError:

means, that the class which you are trying to run was not found in the classpath.

Solution: you need to add the class or .jar file which contains this class into the java classpath. When you are running a java class from the command line, you need to add the dot (.)

java YourSingleClass -cp . 

into the classpath which tells the JVM to search for classes in actual directory.


If you are running a class from a .jar file, you need to add this jar file into the classpath:

java org.somepackage.SomeClass -cp myJarWithSomeClass.jar



 

Comments  

 
-1 #1 bhavin 2010-07-01 13:13
I tried setting in the PATH, CLASSPATH, JAVA_HOME, from going into the Environment variables, but it didnt worked.

But then later i tried this....
set classpath=%classpath%;.;
by going into the folder where my java code is there, and it worked.

Thanks a lot rishi :)
Quote
 
 
-15 #2 preeti 2010-07-08 21:18
m still not able to get it
plz help
:cry:
Quote
 
 
-6 #3 preeti 2010-07-08 22:04
hey it really helped me
thank u all guys
Quote
 
 
+2 #4 sangeetha 2010-07-15 19:25
Quoting rishi:
in command promt go to ur folder whr ur java file exist and write this
set classpath=%classpath%;.;

best of luck for java programming.

thank you soo much rishi it was really helpful.....
Quote
 
 
-4 #5 manisha 2010-07-17 16:25
how to set classpath and java_home plz give me reply
my jdk1.6 installed different folder then my java program
folder
Quote
 
 
-4 #6 amarjit 2010-07-19 18:08
i have not understood this lang exception.kindly explain it by displaying on user screen ,command by command.i hope u got it.waiting for ur reply eagrly.
Quote
 
 
-1 #7 shyamendra 2010-07-21 12:23
Classpath
.;c:\j2sdk1.4.2_04\jre\lib;./;z:\classes

JAVA_HOME
c:\j2sdk1.4.2_04

PATH
.;c:\j2sdk1.4.2_04\bin;

CATALINA_HOME
c:\Tomcat
path will according you java installtion
Quote
 
 
-6 #8 NaviNice 2010-07-23 07:12
Cool Dude.
Nothing Happen strange...
just go to system environment and remove all path variable which include java path.....
now again create a new path variable and paste there java bin path...
which is usual C:\Program Files\Java\jdk1.6.0_19\bin :lol:
Now It will Work ..........
The problem behind this error is that there are multiple java path variable in system environment variable . . .
Or
you have different version of jdk or jre
Quote
 
 
-1 #9 NaviNice 2010-07-23 07:31
:lol: Cool Dude
Nothing happen strange.
you just right click on "my compute"r icon on desktop.
Go to "properties".
Then Go to "Advanced".
Then go to "environment variables"
where the java path is set in path or may be classpath or JAVA_HOME ..
Delete all java path..
Now again take a new path variable and set java path in it which is mostly C:\Program Files\Java\jdk1.6.0_19\bin
then save it. . . . . . . .

Now What is the problem behind this error..
1. multiple path variables with java path
2. you have different JRE and JDK version



Njy
Quote
 
 
-3 #10 Swamy 2010-07-26 02:48
Hi

I am a newbie. I too faced the same problem. It worked fine in the command prompt as I have set the class path right. But , in Eclipse if i give the class name as hello.java or hello.class i got the error. When i tried using hello it worked like magic. I understood now. :-)
Quote
 
 
-4 #11 Mani Prakash 2010-07-27 11:50
Thanks a lot. it works
Quote
 
 
-4 #12 Kanchan 2010-07-29 07:25
Thank You Very Much. I was trying to resolve this problem but didn.t succed.
ur solution helps me lot. :lol: , :D :D
Quote
 
 
-3 #13 K@rtik 2010-07-29 20:23
actually when u install java u only require to set its path
, u rally not need to set its CLASSPATH and JAVA_HOME variable because by setting path to its jdk's bin direcotry it automaiticlly treat and compile and run from any folder or directory
Quote
 
 
-2 #14 Satyadeep 2010-08-07 19:50
:-) Thank you very much
That had made my day
Quote
 
 
-3 #15 unknownq 2010-08-12 12:34
Quoting Deepak:
It was really helpful. Thanks a lot. 8)
really worked
Quote
 
 
-6 #16 Neeraj 2010-08-27 10:39
Thanks a lot..it really worked..
Quote
 
 
-5 #17 Mitul_Patel 2010-09-01 05:59
Thanks man great help
Quote
 
 
-3 #18 Avinash Bikumalla 2010-09-03 03:14
Thanks for the solution :-) :-)
Quote
 
 
0 #19 JayB 2010-09-09 18:41
Hey I found this interesting. All of this didn't help me, but I had the same error. The problem for me was that I was using an external IDE and had missed a package declaration at the top of the page. I remove it and viola! XD
Quote
 
 
+1 #20 C12Simone 2010-09-13 14:35
CLASSPATH
C:\Program Files\Java\jre6 \lib\ext\QTJava .zip;

Add a semi-colon(;) at the end of the classpath.
This really Works for me for java 6.
Quote
 
 
0 #21 Cem Arslan 2010-09-15 00:15
(1) javac HelloWorld.java
(2) java HelloWorld

java helloworld ---> will not work you have write same class name... if it is HelloWorld write like that if it is HeLLoWORLD write like that
Quote
 
 
-2 #22 Sasmita 2010-10-10 03:46
thanks... a lot :lol: :D :roll:
Quote
 
 
-2 #23 Praddy 2010-10-11 17:47
Helped a lot! thank you! 8)
Quote
 
 
-7 #24 udita 2010-10-14 15:16
ohh ..thnk u soo mucchh .. it was of great help.
Quote
 
 
-5 #25 udita 2010-10-14 15:17
thnxx gr8 help .. :)
Quote
 
 
-1 #26 Vani 2010-10-18 08:05
install jdk then go to my computer properties advance option their select environment select new set jdk bin directory path click ok....

in command prompt go to ur folder whr ur java file exist and write this
set classpath=%classpath%;.;

surely it works try this....All the best...
Quote
 
 
0 #27 Snail 2010-10-20 14:50
Do you know where my stupid problem is?
I put a ',' instead of ';' after '.' in the CLASSPATH :-*
Quote
 
 
-1 #28 sandysam 2010-10-20 16:08
THANKS A LOT!!!!
It was very helpfull!!!!! :D :lol:
Quote
 
 
0 #29 neera singh 2010-12-23 06:12
thanks a ton :lol:
Quote
 
 
0 #30 neera singh 2010-12-23 07:26
thanks a ton it worked.......... :lol:
Quote
 
 
0 #31 Shaahid 2011-01-02 20:22
Thanks a lot for the info. Its working
Quote
 
 
-1 #32 Sadiksha 2011-01-04 07:01
Thanks a lot, it really worked..... :lol: :-) :D
Quote
 
 
-1 #33 Neeraj soni 2011-01-19 06:56
if u get a solution for the above problem u faced, pls provide for me also since i too facing the same
Quote
 
 
-4 #34 Preeti 2011-01-25 09:56
Really helpful.Thanks :)
Quote
 
 
-1 #35 Javin@ TibcoTutorial 2011-01-29 09:37
Hi,

Its worth noting that if you have two classes with same name in classpath in that case one which comes earlier in classpath will get picked up. this concept is very useful to test patch releases where you update only few classes to quickly test patch release or have added some debug print statement to troubleshoot any issue. to read more about How classpath works in Java

Thanks
Javin
Why String is immutable in Java
Quote
 
 
0 #36 landun 2011-02-13 21:02
Apparently it matters in which order you put the class-files when creating your jar-file. If you're using superclasses/subclasses you have to put the superclass first in the command! Like so: "jar cvmf manifest.txt myfile.jar SuperClass.class SubClass.class TestClass.class"
Quote
 
 
-4 #37 vinothtel 2011-02-25 06:04
I connect java with mysql...
At run time exception thread:no classdef found exception: filename
error will be displayed....
Quote
 
 
-1 #38 romasius 2011-03-14 13:54
Can someone explain why Java guys did so (-cp .):
java YourSingleClass -cp .
It will be bether that Java will search automatically for classes in actual directory-directory from which is running application:
java YourSingleClass
Quote
 
 
-1 #39 iamshoaib@gmail.com 2011-04-15 15:00
Thanks man it works for me, I was also forgot to give that dot even I am not new to java, Really helpful note, 10Q
Quote
 
 
-1 #40 thedrs 2011-04-27 09:40
Common mistake that leads to the above error (even when all paths are correct) is doing:

java Hello.class

instead of actually doing the correct:

java Hello
Quote
 
 
-1 #41 sanarafaai 2011-04-28 10:38
thnkuuuuuuuu sooooooooo much...it really worked....GOD BLESS U....:) JAZAAK ALLAH
Quote
 
 
-1 #42 jusC 2011-05-02 08:30
it worked for me too

cool
Quote