The explanation below is an abbreviation of Sun's tutorial page: http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/classpath.html
-classpath
option when calling an
SDK tool (the preferred method) CLASSPATH
environment variable. -classpath
option is
preferred;
you can set it individually for each application without
affecting other applications and without other applications modifying
its value.
%
sdkTool-classpath
classpath1:
classpath2...where sdkTool is A command-line tool, such as
java
,javac
, orjavadoc
.
- classpath1
:
classpath2
- For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.
- For .class files in an unnamed package, the class path ends with the directory that contains the .class files.
- For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).
-classpath
command-line option
overrides that default: If you want to include the current directory
in the search path, include "." in the settings.
- The Java tools java, jdb, javac, and javah have a
-classpath
option which replaces the path or paths specified by theCLASSPATH
environment variable while the tool runs.
- This is the recommended option for changing class path settings, because each application can have the class path it needs without interfering with any other application.
- The runtime tool java has a
-cp
option, as well. This option is an abbreviation for-classpath
.
java.awt.Button
is always specified as java.awt
. Cool.class
in the package utility.myapp
.
/java/MyClasses/utility/myapp
,
you set the class path so that it contains /java/MyClasses
.
% java -classpath /java/MyClasses utility.myapp.Cool
utility.myapp
package
that are used by the Cool
class. /java/MyClasses/utility
and use the command java myapp.Cool
. The class would not
be found. (The package name is part of the class and cannot be modified, except by recompiling the class.)
Note: An interesting consequence of the package specification mechanism is that files which are part of the same package may actually exist in different directories. The package name will be the same for each class, but the path to each file may start from a different directory in the class path.
/java/MyClasses/utility/myapp
,
the class path entry points to the directory that contains the
first element of the package name. (In this case, /java/MyClasses
,
since the package name is utility.myapp
.) % java -classpath /java/MyClasses/myclasses.jar
utility.myapp.Cool
To find class files in the directory /java/MyClasses
as well as classes in /java/OtherClasses
, you set
the class path to:
% java -classpath /java/MyClasses:/java/OtherClasses ...
/java/MyClasses
./java/OtherClasses
directory.