Java Reference
In-Depth Information
must be stored in …/alpha/beta/gamma, where … specifies the path to the specified direct-
ories.
Finding Packages and CLASSPATH
As just explained, packages are mirrored by directories. This raises an important question:
How does the Java run-time system know where to look for packages that you create? The
answer has three parts. First, by default, the Java run-time system uses the current work-
ing directory as its starting point. Thus, if your package is in a subdirectory of the current
directory, it will be found. Second, you can specify a directory path or paths by setting the
CLASSPATH
environmental variable. Third, you can use the
-classpath
option with
java
and
javac
to specify the path to your classes.
For example, assuming the following package specification:
In order for a program to find
mypack
, one of three things must be true: The program can
be executed from a directory immediately above
mypack
, or
CLASSPATH
must be set
to include the path to
mypack
, or the
-classpath
option must specify the path to
mypack
when the program is run via
java
.
The easiest way to try the examples shown in this topic is to simply create the package
directories below your current development directory, put the
.class
files into the appropri-
ate directories, and then execute the programs from the development directory. This is the
approach used by the following examples.
One last point: To avoid problems, it is best to keep all
.java
and
.class
files associated
with a package in that package's directory. Also, compile each file from the directory above
the package directory.
A Short Package Example
Keeping the preceding discussion in mind, try this short package example. It creates a
simple book database that is contained within a package called
bookpack
.