PDA

View Full Version : NoClassDefFoundError


Stephandeye
07-21-2009, 05:06 AM
I am trying to run two different programs created by classmates, both programs compile but when I try to run them I am getting the following error:

"Exception in thead "main" java.lang.NoClassDefFoundError: <wrong name>
Could not find the main class"

Both classmates insist that their programs run fine on thier computers. Also I have no porblems running my own assignment as well as assignments for other classmates.

I did a bit of research and found on java.sun that the error is telling me the definition of the class file exists at compile time but can no longer be found.

Is there anything that I am doing wrong? Another classmate told me to add -classpath . classname when running it from the command window but this also did not work.

Any advice woud be appreciated. Thanks

ckeyrouz
07-21-2009, 05:38 AM
The problem is there is an incompatibility between the location where you exist in the command prompt and the hierarchy of the java packages you have.

First of all you have to add the package to your classpath.

I will give examples on how you can do it:
Let's suppose that you have a folder named myclasses somewhere under C drive and in it you have the package com.mypackage and in it you have the class you need to execute , name it MyClass.

you can call it in 2 ways:
set your prompt at the folder c:\myclasses:
C:\myclasses> java com.mypackage.MyClass (make sure to call the class using its full path)

or you can be anywhere supposedly on c:\
c:\> java -classpath c:\myclasses com.mypackage.MyClass
here as well you need to call the java class using its full package.

Hope this was useful to you.