wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes NoClassDefFoundError exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoClassDefFoundError exception" Watch "NoClassDefFoundError exception" New topic
Author

NoClassDefFoundError exception

pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Hi all,

I'm running a java program from command prompt, it has passed compilation and when i run i found this exceptions :
C:\Java-source>java Template.java
Exception in thread "main" java.lang.NoClassDefFoundError: Template/java
I have the main.class in the directory, so how come it throws java.lang.NoClassDefFoundError?
Please give me any feedbacks
However, this is my source code

bhuvan sundar
Ranch Hand

Joined: Mar 02, 2005
Posts: 104

Posted by Pingkan
C:\Java-source>java Template.java

just give java Template
pingkan paula
Ranch Hand

Joined: Sep 03, 2004
Posts: 117
Still give the same exception...
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

set classpath to the current working directory.

set CLASSPATH=.; ==> "." represents current working directory.

now, try C:\Java-source>java Template


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Your class is called "Main", so where does the name "Template" come from?

If your class is called "Main", it should be in a source file called "Main.java". After compiling it, you should run it with:

java -cp . Main


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Originally posted by Jesper Young:
If your class is called "Main", it should be in a source file called "Main.java".

This is not a restriction as the class "Main" in your code is NOT a public class. Had it been a public class, it would have been compulsory to do so. However, I also prefer that you should name your file "Main.java", but, it isn't necessary (unless you declare it public.)

Regards,
Abdul Rehman.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If your class isn't public, then recent JDKs will refuse to execute it if it's in a named package. You need to make it public.

Also, don't be setting the classpath; it often just makes things worse down the road. Instead use

java -cp . Template.Main

That's "java space dash cp space Template dot Main" .


[Jess in Action][AskingGoodQuestions]
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Is this behaviour also shown by JDK 1.5? What if I get a question in the SCJP 5 exam or any mock exam about this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoClassDefFoundError exception
 
Similar Threads
Generating random numbers with given properties
Subtract 2 Dates
Sorting array's
The Game of Life - Please HELP!
help with parameter passing