Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

3.21.2008

A Pesky Java Error: NoClassDefFoundError

I've been studying Java through a couple of excellent books, one of them being Thinking in Java (3rd Edition) by Bruce Eckel.

Eckel introduced his own unit testing classes in his sample code. He called the automated testing "SimpleTest". My problem was that the sample Java applications could not find the SimpleTest classes, no matter how hard I tried to configure my environment.

Here was the error I kept getting at run time:

Exception in thread "main" java.lang.NoClassDefFoundError


Solution:
The final solution turned out to be in Eckel's documentation all along, at http://mindview.net/Books/TIJ/. Here's what he said:

Set your CLASSPATH variable to include "." (the current directory) and the code directory.

I had already set my CLASSPATH to include the code directory, but I somehow missed the one about the dot "." so after I tried adding "." in the CLASSPATH and it worked! (For instructions, see below).

Additional information:

To work around the problem, I commented out all references to SimpleTest. And this allowed me to compile and run the sample code. But then I started aiming for test-driven programming, which made me more interested at making SimpleTest work. I read and re-read Eckel's documentation until I finally noticed the instructions about adding the "." to CLASSPATH.

How to set CLASSPATH in Mac OS X

Use a text editor like TextWrangler, which can open hidden files. Navigate to your home directory "~/"and look for a file called .bash_profile. If none, create the file and save it with that file name (no extensions). The file should contain the following line:

export CLASSPATH="/Users/myhome/Development/tij/:."

Explanation:
  • "/Users/myhome/Development/tij" is where my Thinking in Java sample code is saved.
  • The colon ":" is the separator. You can add additional CLASSPATH variables by separating them with a colon.
  • Note the dot "." after the semicolon. This is what I kept missing as I read and reread Eckel's documentation. After I added the dot, everything worked!
Alternatives to changing CLASSPATH in bash

Other programmers advise that it's better to just set the CLASSPATH from the command line, eg, by invoking:
java -classpath
However, this is also tricky, so I prefer to set my CLASSPATH from the environment variables of Mac OS X.

Links:

1.20.2008

Thinking in Java



In preparation for MSIT (Master of Science in Information Technology), I'm brushing up on Java. I scouted around for the best tutorial on this and hit upon Thinking in Java (aka TiJ), by Bruce Eckel.

I've tried Eckel's Thinking in C before and loved his style, so I immediately downloaded the free book. It's in HTML form and available here: http://www.mindview.net/Books/TIJ/. You have the option of buying the hard copy, in which case you get a tutorial CD too!

So now I'll be learning programming Java in a Macbook. Will update this blog about my progress. So far, in the first chapter, most of it is review for me. Eckel is giving the basics of object-oriented programming (OOP) with stuff that I know from UML (objects, classes and types, composition, inheritance etc).

What I like about Eckel's style is that he incorporates what he learned from teaching Java and OOP into the book. So he is intentionally minimizing the concept-loading part by paring down the basics into the most important concepts and then focusing on these at the start.

I remember struggling with a bad Java book during Java 1.0 days. The book was trying to do several things simultaneously on Chapter One -- Java history, syntax, methods, objects, creating instances, etc. Plus the author was trying to explain objects too much, using too many analogies that only made things far harder to grasp.

Eckel simply talks about objects and how they are organized. Then he introduces an example, without overloading the padawan Java programmer's brain. So far, it's fun reading the book.


Extra:
Download this book in HTML format. Free!