Re: Exception in thread "main" java.lang.NoClassDefFoundError
ramif wrote:
Hi,
I have 2 classes: Fibonnaci.java and TestFibonacci.java
In the same dirctory, there is also junit.java
Using Console, i've first typed:
javac -cp ./junit.jar Fibonacci.java TestFibonacci.java
Everthing went fine, until I've typed:
java -cp ./junit.jar Fibonacci
The following error appeared:
Exception in thread "main" java.lang.NoClassDefFoundError: Fibonacci
A NoClassDefFoundError almost always means that your classpath is not
set up properly.
What am I doing wrong? I can't figure out the mistake.
Your classpath is /solely/ junit.jar, and, thus, all searching for
classes will take place in that jar file. [*] However, you want to make
java search in the current directory, which requires you to augment your
classpath.
On Windows, the correct command is:
java -cp .;junit.jar TestFibonacci
and on POSIX-systems (i.e., everything else), it's:
java -cp.:junit.jar TestFibonacci
[*] There is actually a more tortuous list that decides where things are
looked. The brief statement is that things are first searched in the
bootstrap classpath, followed by the extension and endorsed classpaths
(don't recall the exact order there) and finally the classpath. Within
each classpath, the classes are searched for in the specified order.
Most of this information is not needed except in rare and special cases,
the reason why I am putting this in a footnote.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth