Java Reference
In-Depth Information
that it could be used by other programs in this chapter. To compile ListTest.java , use
the command
javac -classpath .;.. ListTest.java
on Windows or the command
javac -classpath .:.. ListTest.java
on UNIX/Linux/Mac OS X. The . in the classpath enables the class loader to locate List-
Test in the current directry. The .. enables the class loader to locate the contents of pack-
age com.deitel.datastructures in the parent directory.
Common Programming Error 21.2
Specifying an explicit classpath eliminates the current directory from the classpath. This
prevents classes in the current directory (including packages in the current directory) from
loading properly. If classes must be loaded from the current directory, include a dot ( . ) in
the classpath to specify the current directory.
Software Engineering Observation 21.2
In general, it's a better practice to use the -classpath option of the compiler, rather than
the CLASSPATH environment variable, to specify the classpath for a program. This enables
each program to have its own classpath.
Error-Prevention Tip 21.2
Specifying the classpath with the CLASSPATH environment variable can cause subtle and
difficult-to-locate errors in programs that use different versions of the same package.
Specifying the Classpath When Executing a Program
When you execute a program, the JVM must be able to locate the .class files for the pro-
gram's classes. Like the compiler, the java command uses a class loader that searches the
standard classes and extension classes first, then searches the classpath (the current direc-
tory by default). The classpath can be specified explicitly by using the same techniques dis-
cussed for the compiler. As with the compiler, it's better to specify an individual program's
classpath via command-line JVM options. You can specify the classpath in the java com-
mand via the -classpath or -cp command-line options, followed by a list of directories
or archive files. Again, if classes must be loaded from the current directory, be sure to in-
clude a dot ( . ) in the classpath to specify the current directory. To execute the ListTest
program, use the following command:
java -classpath .:.. ListTest
You'll need to use similar javac and java commands for each of this chapter's remaining
examples. For more information on the classpath, visit docs.oracle.com/javase/7/
docs/technotes/tools/index.html#general .
21.5 Stacks
A stack is a constrained version of a list— new nodes can be added to and removed from a
stack only at the top . For this reason, a stack is referred to as a last-in, first-out ( LIFO ) data
 
 
Search WWH ::




Custom Search