Register | Log in | RSS channel for codegravity.com homepage Bookmark!

284/2NEW projects | 71 services | 215 websites | 834/3NEW freelancers | 2126 topics | advertise



Login:



Forgot your password?
Forgot your username?
Create an account
JoomlaWatch Agent

Users

Most active users today from total of 108:
garikello, Peerke, jakkrit, dayanand, andrew_saber, giakhanh, mohalb, carmachhh, fguyon, dfireablaze, boho, phoeker, ozedfr, ashish_php, amoluca, blackie, JDev, tinytim007, Pascale, besoil


19.9%United States United States
17.9%India India
9.3%Germany Germany
5.5%Russian Federation Russian Federation
4.4%United Kingdom United Kingdom
4.3%France France
4%Australia Australia
3.8%Netherlands Netherlands
3.8%Poland Poland
3.7%Italy Italy

Today: 953
Yesterday: 1402
This Week: 5243
Last Week: 8869
This Month: 2355
Last Month: 37593
Total: 117035


Partners:

HTML5, CSS3, SVG, tutorials

Freelance ColdFusion, Flex, PHP

Olejomalby, abstraktne obrazy

Camping Europe

WinAsm Studio

Vyšné Ružbachy

Sochy, Reštaurovanie

R.E.M.

Valid XHTML 1.0 Transitional

RSS feed:

Statistics:
Search Engine Genie Promotion Widget
Home

Exception in thread "main" java.lang.NoClassDefFoundError


 


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  

 
+7 #1 Deepak 2008-01-31 14:51
It was really helpful. Thanks a lot. 8)
Quote
 
 
+1 #2 goutam krishna 2008-02-29 17:47
hi...
if u get a solution for the above problem u faced, pls provide for me also since i too facing the same
Quote
 
 
-2 #3 avesh 2008-03-26 06:52
SET PATH=C:\j2sdk1.4.2_05\bin

SET CLASSPATH=C:\j2sdk1.4.2_05\bin

SET JAVA_HOME=C:\j2sdk1.4.2_05

To solve java runtime error type above codes
Quote
 
 
+1 #4 Anil 2008-05-30 13:59
Thanks !
Quote
 
 
+2 #5 rishi 2008-07-04 10:49
in command promt go to ur folder whr ur java file exist and write this
set classpath=%classpath%;.;

best of luck for java programming.
Quote
 
 
+1 #6 ZutoKido 2008-08-03 21:16
It was really helpful! Thanks!!!
Quote
 
 
0 #7 Jessiecca 2008-08-07 13:24
Thanks a lot... it was of great help
Quote
 
 
0 #8 sandhya 2008-08-27 13:54
:D Thanks for this solution
Quote
 
 
+1 #9 aLumma 2008-09-03 01:12
thanks a lot!

what is the "CLASSPATH" variable doing? does it have to read "SET" in front? it doesn't here, and i dont't have the "JAVA_HOME" variable either, should i put it in??

sorry if these questions are beyond stupid, but i'm totally new to java!

.a
Quote
 
 
+1 #10 Anna 2008-09-30 15:44
Thanks a lot!!!!!!!!!!!! That was very helpfull.
Quote
 
 
+2 #11 varvar 2008-10-02 10:18
Thanks a ton dude!
even i was stuck at this silly silly point

Thanks again
Quote
 
 
+4 #12 SEKHAR 2008-10-10 10:17
hi folks ,
thanks for u r useful comments.
but any one here tell me what's the difference between these 3 paths
1.JAVA_HOME
2.CLASSPATH
3.PATH
Quote
 
 
+1 #13 thanks 2008-10-11 11:38
Thanks a lot!
Quote
 
 
0 #14 Shamus 2008-10-22 05:07
MUCHAS GRACIAS!
Quote
 
 
0 #15 Sneha 2008-10-23 02:34
Thank you.. This is really helpfull for me a Newbe to java.
Quote
 
 
0 #16 Patti 2008-10-24 03:06
I did all this, but it doesn't help! This is so frustrating! HELP, please?!
Quote
 
 
0 #17 Ali 2008-10-27 11:14
hi
i have identify an object.
i have add the jar to the netbeans.
i have import the packeg.
.
when i call a class from a jar file i get this error:

The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletExceptio n: #{GetBean.Validate}: javax.faces.el.EvaluationExcep tion: java.lang.NoClassDefFound Error: org/apache/log4j/Logger
javax.faces.webapp.FacesServlet.service(FacesSe rvlet.java:209)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(Monito rFilter.java:368)

root cause

javax.faces.FacesException: #{GetBean.Validate}: javax.faces.el.EvaluationExcep tion: java.lang.NoClassDefFound Error: org/apache/log4j/Logger
com.sun.faces.application.ActionListenerI mpl.processAction(A ctionListenerI mpl.java:78)
javax.faces.component.UICommand.broadcast(UICom mand.java:312)
javax.faces.component.UIViewRoot.broadcastEvents (UIViewRoot.java:267)
javax.faces.component.UIViewRoot.processApplicat ion(UIViewRoot.java:381)
com.sun.faces.lifecycle.InvokeApplicati onPhase.execute(InvokeA pplicati onPhase.java:75)
com.sun.faces.lifecycle.LifecycleImpl.phase(Lifecycle Impl.java:200)
com.sun.faces.lifecycle.LifecycleImpl.execute(Lifecyc leImpl.java:90)
javax.faces.webapp.FacesServlet.service(FacesSe rvlet.java:197)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(Monito rFilter.java:368)



please help......
:cry:
Quote
 
 
-1 #18 nisha 2008-11-10 15:20
Thanx aloooooooot.........its been a great help
Quote
 
 
0 #19 Reaper 2008-11-10 18:32
finally it works, after searching thousands of sites on net, it solved this easy one. thx, thx, thx a lot
Quote
 
 
+1 #20 santosh kumar 2008-11-30 19:43
hi
i getting this problem
Exception in thread "main" java.lang.NoClassDefFound Error

tell how i ll remove this problem
Sponsored links:
Quote
 
 
0 #21 mouse 2008-12-04 20:45
..wtf i just took the .class off and it worked...WHY
that makes no fucking sence...this is why i have hated java for sooo fucking long
Quote
 
 
0 #22 Casttro 2008-12-05 12:53
PATH = The operating system will look into this
CLASSPATH = The application will look into this
Quote
 
 
0 #23 Rishi 2008-12-08 14:18
Thanks a lot for this helpful info :-)
Quote
 
 
-1 #24 viresh 2008-12-08 19:58
classpath are used to set lib files.

path is used to store bin.

java home is just a environment variable which will be useful when u do an application on application server.
Quote
 
 
0 #25 visakh 2008-12-15 09:59
tiny info, but very useful.
Quote
 
 
+1 #26 charu 2008-12-16 07:54
i somehow corrected my path and my prog is compiled successfully.... but now my program is not executing
it says:
D:\Softwares\or acle\product\10.2.0\client_1\jdk\bin>java d:\softwares\ja vaprograms\hell oworld
Exception in thread "main" java.lang.NoClassDefFound Error: d:\softwares\ja vaprograms\hell oworld
Quote
 
 
+2 #27 jyothi 2009-01-15 10:16
hi guys,
m unable to rectify the error even now after doing all alternative techniques.plz help me m gvng my program below:

import java.sql.*;
import java.io.*;
import java.util.*;
public class JDBCExample1{
public static void main(String args[]) throws Exception
{
FileInputStream fis=new FileInputStream ("config.properties");
Properties props=new Properties();
props.load(fis);
String vdriver=props.getProperty("driver");
String vuname=props.getProperty("uname");
String vpwd=props.getProperty("pwd");
String vurl=props.getProperty("url");
Class.forName(vdriver );
Connection con=DriverManager.getConnection(v url,vuname,vpwd );
System.out.println(con.getClass());
con.close();
}
}

config.properties

String driver=Oracle.jdbc.driver.OracleDriver
String uname=system
String pwd=tiger
String url = jdbc:oracle:thi n:@localhost:1521:-xe
Quote
 
 
+1 #28 Song Toan 2009-01-21 11:00
I'm a new guy to Java using JDK 6 in Windows. I stuck here few hours for this issue. None of the solutions found from Internet solved. Finally, I got it worked.

There are 2 ways:

1) Set CLASSPATH environment to C:/Program Files/Java/jdk1.6.0_11/jre/lib;./;
Your jdk installation may be at different location.

2) Use -cp option:
java -cp "C:/Program Files/Java/jdk1.6.0_11/jre/lib;./;"

Pay attention at the "./" at the end of the paths in both solutions.

Conclusion: The java.exe does not take current directory by default! We have to add it into by ourselve!

Hope this save times of many many new guys like me!
Quote
 
 
0 #29 lokesh 2009-01-30 19:46
thank s dude
Quote
 
 
0 #30 Ramya NR 2009-01-30 20:14
Thank u. This was a very useful solution for my long time struggle with the java s/w. Most don't know this logic. thanks again.
Quote
 
 
+2 #31 ashiq 2009-02-03 14:17
I am struck even i change the variables i am getting the same error
i am completely new to java can you help me
the error is:
Exception in thread "main" java.lang.NoClassDefFound Error: Hello
Quote
 
 
0 #32 Dang Tuan 2009-02-13 04:30
I've tried the command: set classpath=%classpath%;.;

It works! Amazing :D

Thanks.
Quote
 
 
+1 #33 KK 2009-03-26 16:47
set classpath=%classpath%;.;

it works thanks a lot
Quote
 
 
0 #34 Shashank Sawant 2009-05-25 10:23
I tried all the solution mentioned above but my problem still persists. Here is what I did in detail:
When I tried to compile my HelloWorldApp.java file using the following command:
javac HelloWorldApp.java
it worked (for being able to do this simple thing I had to change a lot of variables).

But then when I tried to Run the program using:
java HelloWorldApp
it gave me the error:
Exception in thread "main" java.lang.noclassdef...
...
...
...

To solve this error I tried various commands like:
set classpath=%classpath%;.;
java -classpath . HelloWorldApp
java -classpath . HelloWorldApp.class

but to no avail. Can anyone help me out with this problem?

Regards,

-sgsawant
Quote
 
 
0 #35 Gajanan 2009-09-04 01:58
Thanks so much I had same problem. Really stupid problem which can discourage you .
Quote
 
 
0 #36 padmakar vishe 2009-09-25 22:58
Thanks...a....lot... :-)
Quote
 
 
0 #37 dharmendra 2009-10-03 14:48
many many thanks,
Quote
 
 
0 #38 nano 2009-10-07 11:42
the problem was the missing dot "."
without dot, I think the JVM does not feel like searching the current working directory for the class.
YES, this can surely discourage the JAVA newbies.
I insanely wasted 1 hour over this problem in uninstalling and reinstalling the JAVA SE.
Quote
 
 
-1 #39 FARAZ 2009-10-16 12:04
I SOLVE THIS ERROR
HOW?
WAIT FOR MY NEXT COMMENT

FARAZ
Quote
 
 
0 #40 Elridge D 2009-10-30 22:28
I was seeing this error even though i had the correct environment variables (PATH and JAVA_HOME).

I found that the issue had to do with where I was compiling my test class. I had it in a package (com.xyz.apps.test.setup), so the fix for me was that I needed to run the javac command at the same level as where the /com directory existed. I had tried to compile it from the level of where the class lived (inside /com/xyz/apps/test/setup). Also, i specified a destination directory and then ran the java command on the compiled class in that directory.
Quote
 
 
0 #41 Jack B 2009-11-06 03:05
Thank you sooo much
Quote
 
 
+4 #42 hmmmmmmm 2009-11-25 08:36
i still dont get where to put these.. i put the CLASSPATH=.;c:\programme\j boss\lib;c:\pro gramme\
j2sdk1.4.2_08\lib;c:\pro gramme\j 2sdk1.4.2_08\jre\lib; in the variable name but whats the value.. i vid tut would be nice if you could make me one thanks :-)
Quote
 
 
0 #43 Abhi 2009-11-27 00:31
Thanks a lot!!! :-)
Quote
 
 
0 #44 sms 2009-11-30 08:16
It was really helpful! Thanks!!!
Quote
 
 
-1 #45 sms 2009-12-01 10:58
Error
finally it works, after searching thousands of sites on net, it solved this easy one. thx, thx, thx a lot
Quote
 
 
0 #46 Prasad 2010-02-26 16:31
Just found out one more problem while setting up CLASSPATH
SET CLASSPATH=C:/Program Files;
and
SET CLASSPATH="C:/Program Files"; BEHAVES very differently while executing from command prompt but works fine if environment variable is set with out text qualifier ("") and IDE is used for execution
If there is space between (Like Program Files) use text qualifier “C:\Program Files\Cognos\c8 \webapps\p2pd\W EB-INF\lib” for path instead of C:\Program Files\Cognos\c8 \webapps\p2pd\W EB-INF\lib
Quote
 
 
0 #47 pradeep 2010-04-06 08:09
the easy but illogical way of solving this
[Exception in thread "main" java.lang.NoClassDefFound Error:]
is to delete the space in between class and name and again leaving a space in between them . now save it and try it
(solves only if the path is in bin)
Quote
 
 
0 #48 Hassan 2010-04-24 15:37
I TRIED ALL ABOVE COMMAND BUT THE SAME PROBLEM IN JAVA.LANG
Quote
 
 
0 #49 Hi Thanks for valuable comment 2010-05-05 12:34
File 1: Goo.java


import cert.*;
package book;


class Goo
{

public static void main (String [] args)
{
Sludge o = new Sludge();
o.testIt();
}
}




File 2: Sludge.java


package cert;

public class Sludge
{
public void testIt()
{

System.out.println("Sludge");
}
}


Here i have 2 files in different package called cert & book.

My files are compiled but when i try to run Goo . it gives error , although method in both prorgram are public accessifier.

Pls. Suggest.
Quote
 
 
0 #50 Hi Thanks for valuable comment 2010-05-05 12:35
File 1: Goo.java


import cert.*;
package book;


class Goo
{

public static void main (String [] args)
{
Sludge o = new Sludge();
o.testIt();
}
}




File 2: Sludge.java


package cert;

public class Sludge
{
public void testIt()
{

System.out.println("Sludge");
}
}


Here i have 2 files in different package called cert & book.

My files are compiled but when i try to run Goo . it gives error , although method in both prorgram are public accessifier.

Pls. Suggest.
Quote
 
 
0 #51 zero to hero 2010-05-29 23:03
your cut to cut approach of telling is gr8 i benifited frm you rather i was wasting so much tym in other websites
Quote
 
 
+1 #52 bhavin 2010-07-01 14: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
 
 
0 #53 preeti 2010-07-08 22:18
m still not able to get it
plz help
:cry:
Quote
 
 
0 #54 preeti 2010-07-08 23:04
hey it really helped me
thank u all guys
Quote
 
 
0 #55 sangeetha 2010-07-15 20: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
 
 
0 #56 manisha 2010-07-17 17:25
how to set classpath and java_home plz give me reply
my jdk1.6 installed different folder then my java program
folder
Quote
 
 
0 #57 amarjit 2010-07-19 19: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
 
 
0 #58 shyamendra 2010-07-21 13: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
 
 
0 #59 NaviNice 2010-07-23 08: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
 
 
0 #60 NaviNice 2010-07-23 08: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
 
 
0 #61 Swamy 2010-07-26 03: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
 
 
0 #62 Mani Prakash 2010-07-27 12:50
Thanks a lot. it works
Quote
 
 
0 #63 Kanchan 2010-07-29 08: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
 
 
0 #64 K@rtik 2010-07-29 21: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
 
 
0 #65 Satyadeep 2010-08-07 20:50
:-) Thank you very much
That had made my day
Quote
 

Add comment


Security code
Refresh

No Tweets

Add URL of a programming website
register as a freelancer
submit your project
open freelance projects you can work on
webmaster resources
write paid blog posts about programming - tips and articles
bugs and features

Recommended: (advertise)
Download Joomlawatch 1.2.12


FastDomain.com -
Fast Domain hosting. Hostmonster alternative. Also for $6.95/month.

Register as a Freelancer -
Submit your project experiences, programming languages and your other skills


Freelancers, create paid articles
-
Looking for freelancers with experiences in Java to write paid articles on their experiences


Advertise Here
-
Promote your services and get the new customers from the community of webmasters / programmers visiting CodeGravity.com

Green Geeks Hosting -
World's best selling green web hosting

HostMonster.com -
best cheap Joomla Web hosting. Website joomlawatchdemo.com is hosted by them with domain for $6,95/mo.


Outsource your project
-
Submit your project description with all the requirements to more than 600 freelancers from around the world for FREE.



News from Twitter:
No Tweets
RSS Feeds:new

rss Freelance
rss Projects
rss Forum
rss Resources

Popular:
MathGuard
download mathguard
Random screenshot:

linoopz15.jpg


Poll

At what price would you purchase JoomlaWatch ad-free license?
 
Privacy policy | Advertise | Donate | Doucovanie Nemcina
Joomla Visitors Google Map
Loading map...
Joomla Visitors Google Map
Visitors Google Map Agent

Locations of visitors to this page


©2003-2010 Codegravity.com