• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

NoClassDefFoundError exception

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still give the same exception...
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic