Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Environment Setup Problem - OS X 10.4.3, Java 1.5.0 - Running from Terminal

 
Marcus Peaston
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

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?

All suggestions are gratefully received,

Regards,

Marcus
 
Loren Rosen
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things that might be going on.

classpaths have elements separated by colons ( the : character). But in the command line example you have
java -cp . path.to.class.package
instead of
java -cp . ath.to.class.package

Second, check the structure of your jar file, using 'jar t'. what often happens is that the class files are there in the jar but path to it inside the jar isn't what it should be.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, here's a neat GUI for setting environment variables on Mac...

http://www.rubicode.com/Software/RCEnvironment/
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic