Java Reference
In-Depth Information
Set the
CLASSPATH
variable equal to the directory location of the user-defined Java
classes or Java Archive (JAR) files that you need to have access to for executing your
application. Let's say that you have a directory named
JAVA_DEV
located at the root
of your OS drive, and all the files your applications needs to access are located in this
directory. If this is the case, then you would execute a command such as the following:
set CLASSPATH=C:\JAVA_DEV\some-jar.jar
Or on Unix and Linux systems:
export CLASSPATH=/JAVA_DEV/some-jar.jar
Alternately, the
javac
command provides an option for specifying the location of
resources that need to be loaded for an application. On all platforms, setting the
CLASSPATH
using this technique can be done via the
-classpath
option as fol-
lows:
javac -classpath /JAVA_DEV/some-jar.jar
Of course, on Microsoft Windows machines the file path will use the backslash (
\
)
instead.
Note
The
javac -cp
option may be used, rather than specifying the
-classpath
option.
How It Works
Java implements the concept of a
class path
. This is a directory search path that you
can specify system-wide using the
CLASSPATH
environment variable. You can also
specify the class path for a specific invocation of the JVM via the
java
command's
-
classpath
option. (See Recipe 1-3 for an example.)
When executing Java programs, the JVM finds and loads classes as needed using
the following search order:
1.
The classes that are fundamental to the Java platform and are con-
tained in the Java installation directory.