• 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

Getting Exception while running a class file.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am able to compile .java file, but while executing the .class file, i am getting following exception.

Exception in thread "main" java.lang.NoClassDefFoundError: Example
Caused by: java.lang.ClassNotFoundException: Example
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
Could not find the main class: Example. Program will exit.

Please tell, what can be cause for this??
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your class needs to be in the classpath. If you are executing it from the same directory, then you can use "java -cp . Example" to tell the JVM that the current directory should be in the classpath.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey hi,

you must save your program with the same name as your class name.
if you are taking your class name as Test and while saving your writing test.java
program will compile without any error but while execution it will give such exception.

Cheers!
 
Ranch Hand
Posts: 48
1
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swati Save wrote:
you must save your program with the same name as your class name.
if you are taking your class name as Test and while saving your writing test.java



This is not necessary. Its only needed if your class is public. Just lookout for the class which contains main method. You should use the classpath switch -cp while running the class if you are getting such exception.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swati Save wrote: . . . if you are taking your class name as Test and while saving your writing test.java
program will compile . . .

No it won't. Java is case-sensitive and will complain about the difference between test and Test.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:Your class needs to be in the classpath. If you are executing it from the same directory, then you can use "java -cp . Example" to tell the JVM that the current directory should be in the classpath.



Hi,
Here is the program,

public class Example {
public static void main (String args[]) {
System.out.println("This is a simple Java program.");
}
}


I have saved this file with name "Example.java". I am able to get "Example.class". But JVM is not running bytecode & giving that exception.
Can you please tell how to confirm, that Example class is in classpath or not??


Regards,
Vishu
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you set a system CLASSPATH variable? What operating system are you using? Have a look at this "Common Problems" link, and use ctrl-F to find the error you are suffering. It tells you how to get rid of your CLASSSPATH (for the lifetime of the current command prompt). Does that help at all?
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Have you set a system CLASSPATH variable? What operating system are you using? Have a look at this "Common Problems" link, and use ctrl-F to find the error you are suffering. It tells you how to get rid of your CLASSSPATH (for the lifetime of the current command prompt). Does that help at all?



Hi,

Classpath is set to C:\Program Files\Java\jdk1.6.0_17\lib. OS is Windows XP. my class file is getting created at desktop, where i have kept my .java file.
So, you want to say that only classpath is causing this error. Right??

Regards,
Vishu
09988883238
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Delete that CLASSPATH entirely. I presume you have your PATH set correctly otherwise you would have "javac is not recognized . . ." errors.

Then try again.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Delete that CLASSPATH entirely. I presume you have your PATH set correctly otherwise you would have "javac is not recognized . . ." errors.

Then try again.



Hi,
It worked. Thanks

Regards,
Vishu
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done, and "you're welcome"
 
reply
    Bookmark Topic Watch Topic
  • New Topic