• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError exception

 
pingkan paula
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

just give java Template
 
pingkan paula
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still give the same exception...
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
set classpath to the current working directory.

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

now, try C:\Java-source>java Template
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" .
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic