Java Reference
In-Depth Information
PITFALL: Not Including the Current Directory in Your Class Path
Your CLASSPATH variable allows you to list more than one directory. Most operating
systems use the dot to indicate the current directory. The current directory is not any
one specific directory; it is the directory in which you are currently “located.” If you do
not know what your current directory is, then it is probably the directory that contains
the class you are writing. For example, the following value for a CLASSPATH variable
lists two ordinary directories and the current directory:
current
directory
\libraries\newlibraries;\otherstuff\specialjava;.
Whenever you set or change the CLASSPATH variable, be sure to include the cur-
rent directory as one of the alternatives. With the above displayed CLASSPATH
value, if the package is not found by starting in either of the previous two directories,
Java will look in the subdirectories of the current directory. If you want Java to check
the current directory before the other directories on the CLASSPATH variable, list
the current directory (the dot) first, as follows:
.;\libraries\newlibraries;\otherstuff\specialjava
When looking for a package, Java tries the directories in the class path in order and
uses the first one that works.
Omitting the current directory from the CLASSPATH variable can interfere with
running Java programs, regardless of whether or not the programs use packages. If the
current directory is omitted, then Java may not even be able to find the .class file for
the program itself, so you may not be able to run any programs at all. Thus, if you do
set the CLASSPATH variable, it is critical that you include the current directory in the
CLASSPATH. No such problems will occur if you have not set the CLASSPATH vari-
able at all; it arises only if you decide to set the CLASSPATH variable.
If you are having problems setting the CLASSPATH variable, one interim solution
is to delete the CLASSPATH variable completely and to keep all the class files for one
program in the same directory. This will allow you to still do some work while you seek
advice on setting the CLASSPATH variable.
Specifying a Class Path When You Compile
You can specify a class path when you compile a class. To do so, add -classpath fol-
lowed by the class path as illustrated below:
javac -classpath .;C:\libraries\nemeric;C:\otherstuff YourClass.java
This will compile YourClass.java , overriding any CLASSPATH setting, and use
the class path given after -classpath . Note that the directories are separated by
semicolons. If you want classes in the current directory to be available to your
Search WWH ::




Custom Search