Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Oh no! NoClassDefFoundError! Help

 
Michael Chua
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Studying on how to use class imports and came along with a NoClassDefFoundError
I wrote two classes as below, with one using the other

A.java


B.java


I put the files in a directory called test
when I put the B.class file in the test directory, A.java won't compile
but when I put the B.class file in a subdirectory also called test, A compiles
but when I try to run A, it just shows me the NoClassDefFoundError message
If I take away the package test; line in A.java, it also fails compile

what should I do? change classpath? change directories? change code?
thanks in advance.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you take out the import line in A?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take out the "import test.*;" line - you don't need to explicitly import classes that belong to the same package.

Then you need to make sure that package structure matches your directory structure and that the root directory (e.g. in your case, the directory one level above your "test" directory) is included in your classpath. So, if you have C:\projects\test\A.class, make sure C:\projects is included in your classpath.

Finally, when you launch your app, make sure to specify the fully-qualified name, e.g. "java -cp <add your classpath here> test.A"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic