Setting the class path (original) (raw)

In general, you will want to use the -classpath command-line option, as explained in the previous section. This section shows you how to set the CLASSPATH environment variable if you want to do that, or clear settings left over from a previous installation.

In csh, the CLASSPATH environment variable is modified with the **setenv** command. The format is:

**setenv CLASSPATH** path1**:**path2

In sh, the CLASSPATH environment variable can be modified with these commands:

**CLASSPATH =** path1**:**path2**:**...

**export CLASSPATH**

If your CLASSPATH environment variable has been set to a value that is not correct, or if your startup file or script is setting an incorrect path, you can unsetCLASSPATH in csh by using:

**unsetenv CLASSPATH**

In sh, you would use:

**unset CLASSPATH**

These commands unset CLASSPATH for the current shell only. You should also delete or modify your startup settings to ensure that you have the rightCLASSPATH settings in future sessions.

Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. Files will be considered regardless of whether or not they are hidden (that is, have names beginning with '.').

A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo:foo/* or foo/*:foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

Subdirectories are not searched recursively. For example,foo/* looks for JAR files only in foo, not infoo/bar, foo/baz, etc.

The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo containsa.jar, b.jar, and c.jar, then the class path foo/* is expanded intofoo/a.jar:foo/b.jar:foo/c.jar, and that string would be the value of the system property java.class.path.

The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in theClass-Path jar-manifest header.