Полезная информация

Exploring Java

Previous: 3.1 The Java InterpreterChapter 3
Tools of the Trade
Next: 3.3 The Java Compiler

3.2 The Class Path

The concept of a path should be familiar to anyone who has worked on a DOS or UNIX platform. It's a piece of environment information 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 files on the local host platform.

Classes loaded from the local host via the class path have special features. For example, the Java interpreter loads classes in the class path just once; after a core class has been loaded, it can't be modified or replaced. The interpreter can also be told to trust classes in the class path and to load them without passing them through the byte-code verification process. This is important because certain kinds of optimizations on Java virtual machine instructions can produce valid byte-code that, nonetheless, can't pass the verification process. Byte-code that is precompiled on the native host is an extreme example.

The class path is a list of locations where Java class packages are found. A location can be a path such as 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 uncompressed ZIP format. JAR and ZIP are really the same format, but JAR files can carry extra information about their contents. JAR files are created with the jar utility that's part of the JDK; many tools for creating ZIP archives are publicly available. Java automatically looks inside archives and retrieves classes, which then allows large groups of classes to be distributed in a single file.

The precise means and format for setting the class path varies 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:


On a DOS/Windows system, the CLASSPATH environment variable is set with a semicolon-separated list of directories and class archive files:

set CLASSPATH=C:\tools\java\classes.zip;D:\users\vicky\Java\classes;. 

The first example above, for UNIX, specifies a class path with four locations: a ZIP archive in /usr/lib/java, a directory in the user's home, another 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 searches each of these four locations in order to find classes. java expects to find class files in a directory hierarchy or in a directory within a ZIP archive that maps to the fully qualified name of the class. The components of a class-package name become the components of a pathname. Given the above class path, the first time we reference a class with the fully qualified name of animals.birds.BigBird, for example, java begins the search with the classes.zip archive in /usr/lib/java. It looks for a class archived under the path animals/birds/BigBird. If java does not find the class there, it looks for the class in /home/vicky/Java/classes/animals/birds/BigBird. If it's not found there, java moves on to the archive file specified next in the class path, and so on.

For some applications, like the Java interpreter java and the Java compiler javac, the class path can also be specified with the -classpath option:

% javac -classpath /pkg/jdk/lib/classes.zip:/home/pat/mystuff:. Foo.java

If you don't specify the CLASSPATH environment variable or the -classpath option, java uses the following default class path:

.:$JDK_HOME/classes:$JDK_HOME/lib/classes.zip        UNIX systems 
.;%JDK_HOME%\classes;$%JDK_HOME%\lib\classes.zip     Windows systems 

In this path, $JAVA is the main Java directory on your system. Notice that the current directory (.) is the first location in the default class path; this means 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.

Previous: 3.1 The Java InterpreterExploring JavaNext: 3.3 The Java Compiler
3.1 The Java InterpreterBook Index3.3 The Java Compiler

Other Books in this LibraryJava in a NutshellJava Language ReferenceJava AWTJava Fundamental ClassesExploring Java