java classpath Option

At this point, we should mention the classpath option of both the javac compiler and java interpreter. The format is

-classpath path1;path2...

This provides a list of starting search directories, which can include jar archive files, searched for classes by tools such as the javac and java. We cover the jar utility in Section 7.7.2. For example, suppose the tool is searching for package1.Class1 and the classpath option, for Windows, is

-classpath C:\myjava\myapps;C:\myjava\myjar1.jar

For Unix, we would use a colon and forward slashes as separators. The tool would look for Class1 in directory \myjava\myapps\package1 and in myjar1.jar for package1.Class1. javac (but not java) will, by default, also search in the current directory. There is no need to specify search locations for supplied core classes such as java.lang and java.io. The current directory is specified by a dot (.), which is the default if classpath is not specified.

An alternative to using the -classpath option for each application being compiled or interpreted is to set the CLASSPATH environment variable. The details for setting this are operating system dependent; however, the search locations are specified in the same manner as for the -classpath option.

To display the current CLASSPATH variable, use these commands in Windows and Unix (Bourne shell):

In Windows: C:\> set CLASSPATH
In Unix: % echo $CLASSPATH


To delete the current contents of the CLASSPATH variable, use these commands:

In Windows: C:\> set CLASSPATH=
In Unix: % unset CLASSPATH; export CLASSPATH


To set the CLASSPATH variable, use these commands (for example):

In Windows: C:\> set CLASSPATH=C:\users\george\java\classes
In Unix: % CLASSPATH=/home/george/java/classes; export CLASSPATH

No comments:

Post a Comment