File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "NoClassDefFoundError" Watch "NoClassDefFoundError" New topic


songtao zhang

Joined: Jan 24, 2004
Posts: 6
Hello, my program have been compiled but when I try to run it, the computer show me following error. Could anybody please help me to solve this problem?
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name:
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.ClassLoader.loadClassInternal(
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

This error is telling you that you that the Java Virtual Machine cannot find the class you are trying to run. The �wrong name� portion of the message indicates you are not properly identifying your class when trying to run it because it is part of a package. This is something that many beginners have a some trouble with.
Take a look at the following class:

Notice that class is part of the package ��. If I try to run this class with the com\foo directory as my classpath entry, I will get a similar error as you are getting:

On my system, as most systems, my system�s classpath has an entry of . (just a dot) which means current directory. So if I do the following, I get the same error since the working directory of �D:\Dev\Misc\com\foo\� becomes part of my classpath:

The reason is that the full and proper name of the class is not �MyClass� but rather ��. Therefore I need to identify it as such, and have the directory that contains the package, not the class, in my classpath:

Or if I rely on my system�s classpath entry to leverage the current working directory:

Lastly, as a side note, instead of identifying the (full name of the) class with dot notation, you can use also use forward slashes (not the backslashes that Windows uses) like this:

However, the dot notation is more standard and generally preferred (and just makes more sense since the true name of the class is �� and not �com/foo/MyClass�.
For some more information and guidance, take a look at the How To Set The Classpath article in the Java Beginners Faq section of JavaRanch's Frequently Asked Questions page. Also look at Your First Cup of Java on the Sun Tutorial.
For more information on packages look at the Interfaces and Packages lesson on the Sun Tutorial Site.
[ January 24, 2004: Message edited by: Mark Vender ]
songtao zhang

Joined: Jan 24, 2004
Posts: 6
Thank you very very much, you are so cool! I solved the problem according to your message, It have confused me for two days.
I agree. Here's the link:
subject: NoClassDefFoundError
It's not a secret anymore!