Setting the class path

The explanation below is an abbreviation of Sun's tutorial page: http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/classpath.html

Synopsis

The -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, or javadoc


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).

Description

Using the SDK tools' -classpath option

Understanding the class path and package names

% java -classpath /java/MyClasses utility.myapp.Cool

(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.

Folders and archive files

% java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool

Multiple specifications

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 ...

Specification order