The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the-classpathoption when calling a JDK tool (the preferred method) or by setting theCLASSPATHenvironmentvariable. The-classpathoption is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.
%sdkTool-classpathclasspath1:classpath2...-or-
%setenv CLASSPATHclasspath1:classpath2...where:
- sdkTool
- A command-line tool, such as
java,javac,javadoc, orapt. For a listing, seeJDK Tools.- classpath1
:classpath2- Class paths to the .jar, .zip or .class files. Eachclasspath should end with a filename or directory depending on what you are setting the class path to:
- 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 (thefirst package in the full package name).
Multiple path entries are separated by colons.
The default class path is the current directory. Setting the
CLASSPATHvariable or using the-classpathcommand-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.Classpath entries that are neither directories nor archives (.zip or .jar files) are ignored.
The class path tells the JDK tools and applications where to find third-party and user-defined classes -- that is, classes that are notextensionsor 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 searchingthe Java platform (bootstrap) classes, any extension classes, and the class path, in that order. (For details on the search strategy, seeHow Classes Are Found.)Class libraries for most applications will want to takeadvantage of theextensionsmechanism. You only need to set the class path when youwant to load a class that's (a) not in the currentdirectory or in any of its subdirectories, and (b) not in alocation specified by the extensions mechanism.
If you are upgrading from an older version of the JDK, yourstartup settings may include
CLASSPATHsettings that are nolonger needed. You should remove any settings that are notapplication-specific, such asclasses.zip. Some third-party applications that usethe Java Virtual Machine may modify yourCLASSPATHenvironmentvariable to include the libaries they use. Such settings canremain.You can change the class path by using the JDK tools'-classpath option when you invoke the JVM or other JDK tools or by using the
CLASSPATHenvironment variable.Using the-classpathoption is preferredover settingCLASSPATHenvironment variable because you can set it individually for each application without affecting other applications and without other applicationsmodifying its value.Classes can be stored either in directories (folders) or inarchive files. The Java platform classes are stored in
rt.jar. For more details on archivesand information on how the class path works, seeUnderstanding the class path and packagenames near the end of this document.
- Important Note:Some older versions of the JDK software includeda
<jdk-dir>/classesentry in the defaultclass path. That directory exists for use by the JDK software, andshouldnot be used for application classes. Applicationclasses should be placed in a directory outside of theJDK direcotry hierarchy. That way, installing a new JDKdoes not force you toreinstall application classes. For compatibility with olderversions, applications that use the<jdk-dir>/classesdirectory as a classlibrary will run in the current version, but there is noguarantee that they will run in future versions.
The Java toolsjava,jdb,javac, andjavah have a
-classpathoptionwhich replaces the path or paths specified by theCLASSPATHenvironment variable while the tool runs. This is therecommended option for changing class path settings, becauseeach application can have the class path it needs withoutinterfering with any other application.The runtime tooljava has a
-cpoption, as well. This option is an abbreviation for-classpath.For very special cases, bothjava andjavac have options that let you change the path they use to find their own class libraries. The vast majority of users will never to need to use those options, however.
In general, you will want to use the
-classpathcommand-line option, as explained in the previoussection. This section shows you how to set theCLASSPATHenvironment variable if you want to do that, or clear settingsleft over from a previous installation.Setting CLASSPATH
In csh, the
CLASSPATHenvironment variable is modified withthesetenvcommand. The format is:setenv CLASSPATHpath1:path2In sh, the
CLASSPATHenvironment variable can be modified withthese commands:CLASSPATH =path1:path2:...export CLASSPATHClearing CLASSPATH
If your
CLASSPATHenvironment variable has been set to a valuethat is not correct, or if your startup file or script issetting an incorrect path, you can unsetCLASSPATHin csh byusing:unsetenv CLASSPATHIn sh, you would use:
unset CLASSPATHThese commands unset
CLASSPATHfor the current shellonly. You should also delete or modify your startup settingsto ensure that you have the rightCLASSPATHsettings in futuresessions.Changing Startup Settings
If theCLASSPATHvariable is set at system startup, the placeto look for it depends on the shell you are running:
Shell Startup Script csh, tcsh Examine your .cshrcfile for thesetenvcommand.sh, ksh Examine your .profilefilefor theexportcommand.
Java classes are organized into packages which are mapped todirectories in the file system. But, unlike the file system,whenever you specify a package name, you specify thewhole package name -- never part of it. For example,the package name for
java.awt.Buttonisalwaysspecified asjava.awt.For example, suppose you want the Java runtime to find a classnamed
Cool.classin the packageutility.myapp. If the path to that directoryis/java/MyClasses/utility/myapp, youwould set the class path so that it contains/java/MyClasses.To run that app, you could use the following JVM command:
%java -classpath /java/MyClasses utility.myapp.CoolWhen the app runs, the JVM uses the class path settings tofind any other classes defined in the
utility.myapppackage that are used by theCoolclass.Note that the entire package name is specified in the command. It is not possible, for example, to set the class path so itcontains
/java/MyClasses/utilityand use the commandjava myapp.Cool. The class would not be found.(You may be wondering what defines the package name for aclass. The answer is that the package name is part of theclass and cannot be modified, except by recompiling theclass.)
Note:An interesting consequence of the packagespecification mechanism is that files which are part of thesame package may actually exist in different directories. Thepackage name will be the same for each class, but the path toeach file may start from a different directory in the classpath.
Folders and archive files
When classes are stored in a directory (folder), like
/java/MyClasses/utility/myapp, then the class path entry points to the directory that contains thefirst element of the package name. (in this case,/java/MyClasses, since the package nameisutility.myapp.)But when classes are stored in an archive file (a .zip or .jarfile) the class path entry is the path to and including the .zip or .jar file. For example, to use a class library that is in a .jarfile, the command would look something like this:
%java -classpath /java/MyClasses/myclasses.jar utility.myapp.CoolMultiple specifications
To find class files in the directory
/java/MyClassesas well as classesin/java/OtherClasses, you would set the class path to:%java -classpath /java/MyClasses:/java/OtherClasses ...Note that the two paths are separated by a colon.
Specification order
The order in which you specify multiple class path entries isimportant. The Java interpreter will look for classes in thedirectories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory
/java/MyClasses. Only if it doesn't find aclass with the proper name in that directory will theinterpreter look in the/java/OtherClassesdirectory.
![]() |