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
current 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 fi nd the .class fi le
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
variable 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 fi les 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
followed by the class path as illustrated in the following:
javac -classpath .;C:\lib\numeric;C:\otherstuff YourClass.java
In a UNIX environment, replace the semicolons with colons. 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
 
Search WWH ::




Custom Search