The concept of a
path
should be familiar to
anyone who has worked on a DOS or Unix
platform. It’s an environment variable that provides an
application with a list of places to look for some resource. The most
common example is a path for executable programs. In a
Unix shell, the
PATH
environment variable is a colon-separated
list of directories that are searched, in order, when the user types
the name of a command. The Java CLASSPATH
environment variable, similarly, is a list of locations that can be
searched for packages containing Java class files. Both the Java
interpreter and the Java compiler use CLASSPATH
when searching for packages and classes on the local host.
A location on the class path can be a
directory name or the name of a class archive
file. Java supports archives of class files in its own
Java archive ( JAR) format, and in the conventional
ZIP
format. JAR and ZIP are really the same format, but JAR archives
include extra files that describe each archive’s contents. JAR
files are created with the SDK’s
jar
utility; many
tools for creating ZIP archives are publicly available. The archive
format enables large groups of classes to be distributed in a single
file; the Java interpreter automatically extracts individual class
files from an archive, as needed.
The precise means and format for setting the class path vary from
system to system. On a
Unix system, you set the
CLASSPATH
environment variable with a
colon-separated list of directories and class archive files:
CLASSPATH=/home/vicky/Java/classes:/home/josh/oldstuff/foo.zip:.
On a Windows system, the
CLASSPATH
environment variable is set with a
semicolon-separated list of directories and class archive files:
set CLASSPATH=D:usersvickyJavaclasses;.
The first example above, for Unix, specifies a class path with three
locations: a directory
in the user’s home, a ZIP file in another user’s
directory, and the current directory, which is always specified with
a dot (.
). The last component of the class path,
the current directory, is useful when tinkering with classes, but as
a general rule, it’s bad practice to put the current directory
in any kind of path.
The
Java interpreter and the other
command-line tools also know how to find
core
classes, which are the classes
included in every Java installation. The classes in the
java.lang
, java.io
,
java.net
, and javax.swing
packages, for example, are all core classes. You don’t need to
include these classes in your class path; the Java interpreter and
the other tools can find them by themselves.
To find other classes, the Java interpreter searches the locations on
the class path in order. The search combines the path location and
the fully qualified class name. For example, consider a search for
the class animals.birds.BigBird
. Searching the
class path directory /usr/lib/java
means the
interpreter looks for an individual class file at
/usr/lib/java/animals/birds/BigBird.class
.
Searching a ZIP or JAR archive on the class path, say
/home/vicky/Java/utils/classutils.jar
, means
that the interpreter looks for component file
animals/birds/BigBird.class
in the archive.
For the Java interpreter, java
, and the Java
compiler, javac
, the class path can also be
specified with the
-classpath
option:
% javac -classpath /pkg/sdk/lib/classes.zip:/home/pat/java:. Foo.java
If you
don’t specify the CLASSPATH
environment
variable, it defaults to the current directory
(.
); this means that the files in your current
directory are always available. If you change the class path and
don’t include the current directory, these files will
no
longer be accessible.