I've been having real problems recently running Java applications from the Terminal that can be summed up with the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError
As best as I can tell, for some reason my CLASSPATH environment variable had become incorrect. Oddly, even running the applications using the command "java -cp . path.to.class.package" didn't work all the time, by which I mean that it would work initially and then I'd start getting the NoClassDefFoundError exception until I restarted the Terminal. Strange, no?
At the present time everything seems to work OK if I run my applications from within Eclipse 3.1 and adding "." to the end of the CLASSPATH environment variable seems to have sorted most problems out. However, I am still getting the NoClassDefFoundError exception when attempting to run an application that makes use of an external JAR. Again, it works fine in Eclipse but it doesn't seem to export the JAR into the output directory despite it having been flagged in the "Build Path > Order and Export" options in Eclipse. Copying the JAR file into the output directory and running Java still results in the exception. Not sure if the above is clear but perhaps the following directory tree will help:
bin | |- net | | | |-kelmon | | | |- MyTestClass.class | |- ExternalJar.jar
Running the command "java net.kelmon.MyTestClass" results in the message "Exception in thread "main" java.lang.NoClassDefFoundError: com/externaljar/SomeClass at net.kelmon.MyTestClass.<init>". Given that the JAR file is sat in the current working directory and that the current working directory is specified on the CLASSPATH, shouldn't the classes in the JAR file be automatically available, or am I doing something wrong?