Java Reference
In-Depth Information
The vague “some file(s) in the JDK directory” is release-dependent. You should not mess
with the JDK files, but if you're curious, you can find them in the System Properties under
sun.boot.class.path (see Getting Information from System Properties for System Proper-
ties information).
Suppose you had also installed the JAR file containing the supporting classes for programs
from this topic, darwinsys-api.jar (the actual filename if you download it may have a version
number as part of the filename). You might then set your CLASSPATH to
C:\classes;C:\classes\darwinsys-api.jar;. on Windows or ~/classes:~/classes/darwinsys-
api.jar:. on Unix. Notice that you do need to list the JAR file explicitly. Unlike a single class
file, placing a JAR file into a directory listed in your CLASSPATH does not suffice to make
it available.
Note that certain specialized programs (such as a web server running a Java EE Servlet con-
tainer) may not use either bootpath or CLASSPATH as shown; these application servers typ-
ically provide their own ClassLoader (see Constructing a Class from Scratch with a
ClassLoader for information on class loaders). EE Web containers, for example, set your web
app classpath to include the directory WEB-INF/classes and all the JAR files found under
WEB-INF/lib .
How can you easily generate class files into a directory in your CLASSPATH? The javac
command has a -d dir option, which specifies where the compiler output should go. For ex-
ample, using -d to put the HelloWorld class file into my $HOME/classes directory, I just
type the following (note that from here on I will be using the package name in addition to the
class name, like a good kid):
javac -d $HOME/classes HelloWorld.java
java -cp $HOME/classes starting.HelloWorld
Hello, world!
As long as this directory remains in my CLASSPATH, I can access the class file regardless
of my current directory. That's one of the key benefits of using CLASSPATH.
Managing CLASSPATH can be tricky, particularly when you alternate among several JVMs
(as I do) or when you have multiple directories in which to look for JAR files. Some Linux
distributions have an “alternatives” mechanism for managing this. Otherwise you may want
to use some sort of batch file or shell script to control this. The following is part of the shell
script that I have used—it was written for the standard shell on Unix (should work on Bash,
Ksh, etc.), but similar scripts could be written in other shells or as a DOS batch file:
Search WWH ::




Custom Search