| Author |
NoClassDefFoundError
|
abheeshek reddy
Ranch Hand
Joined: Nov 28, 2008
Posts: 39
|
|
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
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
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
Joined: Nov 28, 2008
Posts: 39
|
|
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
|
 |
Anayonkar Shivalkar
Bartender
Joined: Dec 08, 2010
Posts: 1295
|
|
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.
|
Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD)
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
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.
|
 |
Ravi Vanamala
Greenhorn
Joined: Dec 14, 2004
Posts: 15
|
|
|
Along with the PATH, try setting the JAVA_HOME=C:\Program Files\Java\jdk1.6.0_31 in environment "System Variables"
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12909
|
|
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
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
abheeshek reddy
Ranch Hand
Joined: Nov 28, 2008
Posts: 39
|
|
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.
|
 |
 |
|
|
subject: NoClassDefFoundError
|
|
|