分类: Java
2011-06-14 03:09:17
The class path tells JDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found.
The JDK, the JVM and other JDK tools find classes by searching the Java platform (bootstrap) classes, any extension classes, and the class path, in that order. (For details on the search strategy, see .) Class libraries for most applications will want to take advantage of the . You only need to set the class path when you want to load a class that's (a) not in the current directory or in any of its subdirectories, and (b) not in a location specified by the extensions mechanism.
If you are upgrading from an older version of the JDK, your startup settings may include CLASSPATH settings that are no longer needed. You should remove any settings that are not application-specific, such as classes.zip. Some third-party applications that use the Java Virtual Machine may modify your CLASSPATH environment variable to include the libaries they use. Such settings can remain.
You can change the class path by using the JDK tools' when you invoke the JVM or other JDK tools or by using the . Using the -classpath option is preferred over setting CLASSPATH environment variable because you can set it individually for each application without affecting other applications and without other applications modifying its value.
The Java launcher, java, initiates the Java virtual machine. The virtual machine searches for and loads classes in this order:
If the -sourcepath option is not specified, the user class path is also searched for source files.
If the -processorpath option is not specified, the classpath is also searched for annotation processors.
As a special convenience, a class path element containing a basename of *
is considered equivalent to specifying a list of all the
files in the directory with the extension .jar or .JAR.
For example, if directory foo contains a.jar and b.JAR, then the
class path element foo/* is expanded to
A.jar;b.JAR,
except that
the order of jar files is unspecified. All jar files in the specified
directory, even hidden ones, are included in the list. A classpath entry
consisting simply of * expands to a list of all the jar files in the
current directory. The CLASSPATH environment variable, where defined,
will be similarly expanded.
Note: Depending of the configuration of your command line environment, you
may have to quote the wild card character, for example,
javac -cp "*.jar" MyClass.java.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.
Note: Classes found through the classpath may be subject to automatic recompilation if their sources are also found.