Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

what is the "java.lang.NoClassDefFoundError" ?

Thread view: 
xian_hong2046@hotmail.com - 16 Apr 2006 04:33 GMT
Hello,

After I successfully compiled my program and ran it, I received the
message:

"Exception in thread "main" java.lang.NoClassDefFoundError"

I double-checked my program and there's no mistake at all.  This
problem happened after I set the CLASSPATH variable, which had no
setting before.  I also noticed that my other programs that worked
before failed to work any more, with the same exception as shown above.
Could someone please tell me what's the problem and how to solve it?

many thanks,
xian
MysteryMan - 16 Apr 2006 04:51 GMT
Try the following from the command line to run your program:

java -classpath . YourClassName

Don't forget that "." in there.
xian_hong2046@hotmail.com - 16 Apr 2006 05:10 GMT
Many thanks for your reply and with the -classpath option my previous
programs work.  However, my new program,which requires a package, still
fails to work.

My problem is that I have a package called mypackage.mytest, and inside
this package I have MyFirstTest.java that has MyFirstTest class.  Then
in the second file TestTester.java, I import the mypackage.mytest and
use a static method in the MyFirstTest class.  Then I compiled
MyFirstTest and TestTester files separately.  Everything went on
properly until I used java:

java -classpath . TestTester

when I got the previous error message.

Moreover, I set my CLASSPATH to a subdirectory in my home directory.  I
expect that Java interpreter should be able to find it.  Why do I still
need to use "-classpath" option?

In addition, before I ran TestTester I had moved TestTester.class and
MyFirstTest.class into the correct directory (corresponding to the
package name), so I think this should eliminate the possibility that
Java interpreter can't find the .class in the package directory.

Many thanks,
xian
Jon Martin Solaas - 16 Apr 2006 08:46 GMT
> Many thanks for your reply and with the -classpath option my previous
> programs work.  However, my new program,which requires a package, still
[quoted text clipped - 22 lines]
> Many thanks,
> xian

You shouldn't need the -classpath if your $CLASSPATH is correct. But it
seems it is not. And adding the . doesn't help all that much, because
still you haven't gotten the classpath pointing to the package right.

Suppose you have someting like

/home/me/classes/mypackage/mytest/MyFirstTest.class

Then you should have /home/me/classes in classpath if MyFirstTest is
declared to reside in the mypackage.mytest package. You can achieve that
either via -classpath or by setting the CLASSPATH environment variable.
The latter is somewhat more cumbersome in my opinion. You'd still have
to keep the . so eventually it should look something like

java -classpath .:/home/me/classes TestTester

Signature

jon martin solaas

fiNAL.Y - 16 Apr 2006 10:15 GMT
Seen from your email address xian_hong2046@hotmail.com, it seems that
you're a Chinese, aren't you ?
If so, see:

http://community.csdn.net/Expert/TopicView3.asp?id=4657343
xian_hong2046@hotmail.com - 17 Apr 2006 12:42 GMT
Thanks to all of you, I've solved the problem.  There's just one more
problem that I can't figure out why.  I tried to import an entire
package, using the "*" syntax.  However, the compiler complained it
couldn't find the class.  The problem disappeared after I explicitly
imported the classes stored in that package.  Could someone tell me why
this happened please?

Thanks,
xian
Bjorn Abelli - 17 Apr 2006 16:50 GMT
<xian_hong2046@hotmail.com> wrote...

> Thanks to all of you, I've solved the problem.  There's just one more
> problem that I can't figure out why.  I tried to import an entire
> package, using the "*" syntax.  However, the compiler complained it
> couldn't find the class.  The problem disappeared after I explicitly
> imported the classes stored in that package.  Could someone tell me why
> this happened please?

My guess is that you thought that "subpackages" automatically will be
included when you use the *.

If you want to get a better answer, please provide more information of the
problem at hand, in this case:

- The "import part" of your source code.

- The *exact* error message (including what class
  it complains about not to find).

We're good at guessing, but not *that* good...

// Bjorn A
xian_hong2046@hotmail.com - 18 Apr 2006 12:39 GMT
Hello,

Indeed, I should have provided you with more information. :)

I have two simple classes, the first one is:

package mytest;

public class PackageTest1{
   public static void print(){
    System.out.println("hello, in PackageTest1.");
   }
}

Then in the second one I have:

import mytest.*;

public class PackageTest2{
   public static void main(String[] args){
    PackageTest1.print();
   }
}

After I compiled PackageTest1.java and moved its .class into the proper
directory, I tried to compile PackageTest2.java.  The compiler's error
message was:

PackageTest2.java:5: cannot access PackageTest1
bad class file: ./PackageTest1.java
file does not contain class PackageTest1
Please remove or make sure it appears in the correct subdirectory of
the classpath.
       PackageTest1.print();
       ^
1 error

Finally, I replaced the "import mytest.*;" by "import
mytest.PackageTest1;" and everything worked out.

If the compiler could find mytest.PackageTest1, then surely it should
find mytest.*?

Thanks!
xian
Bjorn Abelli - 18 Apr 2006 18:20 GMT
<xian_hong2046@hotmail.com> wrote...

> After I compiled PackageTest1.java and moved its .class
> into the proper directory, I tried to compile PackageTest2.java.
> The compiler's error message was:
>
> PackageTest2.java:5: cannot access PackageTest1
> bad class file: ./PackageTest1.java

There you got it!

It doesn't complain about PackageTest1.class, but on PackageTest1.java.

Move PackageTest1.java out of the classpath...

> If the compiler could find mytest.PackageTest1, then
> surely it should find mytest.*?

AFAIK, it works approximately this way:

When you explicitly declare PackageTest1 to belong to the package, it looks
there first, otherwise it looks through the whole classpath for
PackageTest1.class *AND* PackageTest1.java.

In that case (mytest.*) it isn't sure what package PackageTest1 belongs to,
and searches the whole classpath. Then it found a .java-file in the
*default* package and tries to work with that, but discovers that it defines
a class *not* belonging to the *default* package, hence a "bad class
file"...

// Bjorn A
xian_hong2046@hotmail.com - 19 Apr 2006 11:19 GMT
Thanks a lot!  It worked after I moved .java out of the way! :)

xian
Roedy Green - 16 Apr 2006 05:14 GMT
On 15 Apr 2006 20:33:16 -0700, "xian_hong2046@hotmail.com"
<xian_hong2046@hotmail.com> wrote, quoted or indirectly quoted someone
who said :

>NoClassDefFoundError

see http://mindprod.com/jgloss/caq.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.