Dr Classpath, or "How I Learned to Stop Worrying and Love the Defaults"

Lots of people have trouble setting up their classpath using an environment variable. They might not know how to set up environment variables for their operating system/shell. They may well have a book which gives inappropriate advice for their edition of the JDK - for example, it might tell them to include classes.zip, which isn't part of Java2. They may well have a tutorial giving them advice on the right JDK, but entirely wrong advice.

Fortunately, if you're using Java2 (JDK1.2 or higher) you probably don't need a classpath environment variable at all. At least, if you do need one, that's probably because you've been writing Java for a while and are doing something complicated - in which case you can probably avoid most of the pitfalls on your own.

Java2 VMs look for classes in three places - the bootclasspath which by default contains rt.jar and i18n.jar (for 1.2.2 and 1.3.1 - it looks like for 1.4 the latter will become charsets.jar), the extensions directories and the general classpath. It is the last one which is affected by the CLASSPATH environment variable or specifying -classpath on the command line.

When using extra libraries, it's probably easiest to use the extensions mechanism (follow link above for more information and gotchas) to show the VM where the libraries are. In the common case of then just having one place where other classes are kept (ie one directory structure with your freshly compiled classes), the default value of the classpath is fine - it's just the current directory. Just stick to the guidelines about compiling and running and you should manage easily.

When to change the classpath

There are times when you have multiple logical build trees - if you're developing a library and its client at the same time, for instance. In this case, it becomes worth changing the classpath, either by using the -classpath flag or by setting the CLASSPATH environment variable. In this case, you may well want to include the absolute directory names so that you can be in either tree to run your app. I would strongly urge you not to go changing the classpath unless you really have to though. It's caused a lot of grief to a lot of people in the past.

Back to the main page.