• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NoClassDefFoundError

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

i am using java 1.6.0 version.
The path has been set to my C:\Program Files\Java\jdk1.6.0_31\bin directory.

Problem:

i am getting NoClassDefFoundError for all my java programs which i kept in one of my practice folders
Even though my code is correct still i am below error.


E:\Technical\Practice>java SingletonDemo
Exception in thread "main" java.lang.NoClassDefFoundError: SingletonD
Caused by: java.lang.ClassNotFoundException: SingletonDemo
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: SingletonDemo. Program will exit.


below is my java program


please advice me to get out of this problem.


Thanks
abheeshek
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Please check if the .class file is present in the classpath
2. Hope you first compiled using javac before executing java command.
 
abheeshek reddy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Jai wrote:1. Please check if the .class file is present in the classpath
2. Hope you first compiled using javac before executing java command.





Thanks John for prompt response,
Usual practice is we won't set individual .class files in classpath. However, previously i used to set only path variable in system variables. if required i used set rt.jar file in class path.


thanks


 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abheeshek reddy wrote:

John Jai wrote:1. Please check if the .class file is present in the classpath
2. Hope you first compiled using javac before executing java command.





Thanks John for prompt response,
Usual practice is we won't set individual .class files in classpath. However, previously i used to set only path variable in system variables. if required i used set rt.jar file in class path.


thanks



You don't need to set individual .class files in classpath. Only setting parent directory of package structure would suffice.

I hope this helps.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant the classpath when the java command is executed.

For sample if you do java SingletonDemo from E:\Technical\Practice, the .class file should be inside the folder (given current folder is the default classpath).

So better try with java -cp . SingletonDemo from the directory that holds the class files.
 
Greenhorn
Posts: 16
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Along with the PATH, try setting the JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31 in environment "System Variables"
 
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

abheeshek reddy wrote:The path has been set to my C:\Program Files\Java\jdk1.6.0_31\bin directory.


What the PATH is set to, is irrelevant for this question.

Ravi Vanamala wrote:Along with the PATH, try setting the JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31 in environment "System Variables"


That's not going to solve anything either - the Java compiler and launcher do not use the JAVA_HOME environment variable at all.

NoClassDefFoundError means that your CLASSPATH is not correct, so that Java can't find a class it's looking for.

First, compile your source file with the command: javac SingletonDemo.java

If that succeeds without errors, check that you have a file named SingletonDemo.class in the current directory.

Then run it. Use the -cp option to specify the classpath; make sure the current directory "." is in the classpath:

java -cp . SingletonDemo
 
abheeshek reddy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

abheeshek reddy wrote:The path has been set to my C:\Program Files\Java\jdk1.6.0_31\bin directory.


What the PATH is set to, is irrelevant for this question.

Ravi Vanamala wrote:Along with the PATH, try setting the JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31 in environment "System Variables"


That's not going to solve anything either - the Java compiler and launcher do not use the JAVA_HOME environment variable at all.

NoClassDefFoundError means that your CLASSPATH is not correct, so that Java can't find a class it's looking for.

First, compile your source file with the command: javac SingletonDemo.java

If that succeeds without errors, check that you have a file named SingletonDemo.class in the current directory.

Then run it. Use the -cp option to specify the classpath; make sure the current directory "." is in the classpath:

java -cp . SingletonDemo




Thanks all it worked ... i resolved the problem by setting the current directory in classpath.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic