• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classpath Error  RSS feed

 
Robert Bonslater
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code compiles fine in Command Prompt but when I run it using java:
Exception in thread "main" java.lang.NoClassDefFoundError: book\Goo (wrong name: blah blah blah...

Yet I do the same in TextPad and it compiles and runs perfectly. What's the deal with that. The Classpath, to my understanding, was set correctly so I do understand.

Please help,
Thanks.
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it does compile fine, then it prolly isnt a problem with ur classpath. However, while talking about classpath, have u set ur path and classpath correctly? (you could find your answer to this on the fAQ posted here)

Also, is main method defined correctly? make sure you do that and if it does accept any command line arguments, you are passing them when u actually run the code after compiling it.

Hope this helps.

Have a good one!

-Kiennjal
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
type in your command prompt :
set classpath=;
if still does not work, type java book.Goo

hope this helps
 
Ricky Martaputra
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
type in your command prompt :
set classpath=;
if still does not work, type java book.Goo

hope this helps
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "wrong name" message always means that Java found the class file it was looking for, but the file turned out to contain a different class than it was expecting. This can have several causes:

On Windows, it can be a letter-case issue. If your Java source says "class goo" and you try to run it with "java Goo" you'll see this kind of error.

A more subtle one does indeed have to do with classpath. If you're trying to run a class in a package, then you want Java to find the .class file in a directory named after the package. It should not find the .class file on the CLASSPATH: it should find that directory on the CLASSPATH. So, for example, given your example, there must be a class file named "Goo.class", it must be in a directory "book", and the parent directory of "book" must be on the CLASSPATH. For instance if the file is in C:\JavaStuff\book\Goo.class, then C:\JavaStuff should be on the CLASSPATH, but not C:\JavaStuff\book .

Note that the very best thing for a newbie to do is to not set CLASSPATH at all; the default is great. Just make sure that when you compile and run book/Goo.java or book/Goo.class, your current directory is C:\JavaStuff. Get used to working in that directory, and never "cd"-ing down into the "book" directory itself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!