• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Himai Minh

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

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

|- 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,


Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, here's a neat GUI for setting environment variables on Mac...

No thanks. We have all the government we need. This tiny ad would like you to leave now:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic