A path specifies the name and location of a file on the file system. It starts with the name of the disk or the root of the filesystem and works its way down through various directories until reaches the file. File, directory, and path naming conventions are platform specific. For example a Unix path looks like /home/users/elharo/html/javafaq.html . A DOS/Windows path looks like C:\html\javafaq.htm . A Macintosh path looks like My Hard Drive:html:Java FAQ List v1.1. All three of these examples point to a file. Paths can also point to a directory. For example, /home/users/elharo/html , C:\html, or My Hard Drive:html:.

The character that separates one directory from the next in a path is called the separator character. It is a slash (/) on Unix, a backslash (\) in Windows and a colon (:) on the Mac. You can get its value on a particular platform by looking at the static variable java.io.File.separatorCharacter.

If you actually check this on the Mac, you'll note something funny. java.io.File.separatorCharacter appears to be a slash (/) like on Unix, not a colon like a Mac programmer would expect. Why Java had to be different from every other Mac program in the universe I don't know. This is problematic because Mac file names can include slashes.

The CLASSPATH is an environment variable that contains a list of directories where Java looks for classes referenced in a program. If the CLASSPATH isn't set properly no program wrien in Java will be able to run, and the compiler won't be able to compile. Each entry in this list is separated from the other entries by the java.io.File.pathSeparatorChar. This is semicolon (;) on Windows and a colon (:) on Unix and the Mac. For example

Unix: ~/classes:/usr/local/netscape/classes
Windows: C:\java\classes;C:\netscape\classes
Mac: My Hard Drive/JDK/classes:My Hard Drive/My Project:My Hard Drive/classes

On most platforms, the JDK's java interpreter appends some directories to the CLASSPATH you set manually. These are set relative to where the java interpreter itself is. For example, if the java program is installed in /usr/local/java/bin, then it will append /usr/local/java/classes and /usr/local/java/lib/classes.zip to the CLASSPATH. Another way of thinking about it: if the directory where the java interpreter is installed is $JAVA, then $JAVA/../classes and $JAVA/../lib/classes.zip are automatically in your CLASSPATH.

Java applets and applications aren't self-contained. They need access to other classes to do their work. For instance when you call System.out.println() Java needs to know where to look to find the file that includes the System class.

The directories in the CLASSPATH are where Java starts searching for classes. To find a class Java first changes the periods in the full package-qualified name of the class (e.g. java.util.Date and not just Date) into directory separators (/ on Unix, \ on Windows, : on the Mac). Thus if it wants the java.awt.GridBagLayout class, it looks for the file java/awt/GridBagLayout.class in each of the root directories listed in the CLASSPATH variable from left to right until it finds the file. With the Unix CLASSPATH listed above, Java first looks for ~/classes/java/awt/GridBagLayout.class Then for, /usr/local/netscape/classes/java/awt/GridBagLayout.class.

The specification of the CLASSPATH is somewhat platform dependent. For instance ~ means the home directory on Unix but has no meaning on the Mac.

Under Unix you set CLASSPATH variables like this:

csh: % setenv CLASSPATH my_class_path
sh: % CLASSPATH=my_class_path

You'll probably want to add one of these lines to your .login or .cshrc file so it will be automatically set every time.

Under Windows you set the CLASSPATH environment variable with a DOS command like

C:\> SET CLASSPATH=C:\JDK\JAVA\CLASSES;c:\java\lib\classes.zip

You can also add this to your autoexec.bat file. You should of course point it at whichever directories actually contain your classes.

The CLASSPATH variable is also important when you run Java applets, not just when you compile them. It tells the web browser or applet viewer where it should look to find the referenced .class files. If the CLASSPATH is set improperly, you'll probably see messages like "Applet could not start."

Since large packages can contain many, many .class files Sun has built the capability to read zip archives into Java. Therefore an entire directory structure of class files can be zipped to save space. If you want to see what's inside the zip file, unzip it. Java doesn't care whether or not a directory has been zipped. You just need to make sure that the .zip file is named the same as the directory it replaces plus the .zip extension and that it is in the same location.

In Netscape you should make sure that the first directory in the CLASSPATH is the directory that contains Netscape's class files (The defaults are /usr/local/netscape/java/classes on Unix and C:\NETSCAPE\NAVIGATOR\Program\java\classes in Windows.)

Finally note that if you install additional packages such as Jeeves or any third party package, you need to add the directory where the package is installed to your CLASSPATH. For example let's say you buy a package of statistics classes from SPSS, and you put those classes in /opt/classes/stats . Then you you need to add /opt/classes/stats to the end of your CLASSPATH.

You can temporarily add a directory to the CLASSPATH by giving the -classpath option to the java interpreter or the javac compiler. For example,

javac -classpath $CLASSPATH:/opt/classes/stats

To use just the classes in /opt/classes/stats and not the classes normally found in your CLASSPATH, omit $CLASSPATH like this:

javac -classpath /opt/classes/stats

Finally if the CLASSPATH environment variable has not been set, and you do not specify one on the command line, then Java sets the CLASSPATH to the default:

Unix: .:$JAVA/classes:$JAVA/lib/classes.zip
Windows: .:$JAVA\classes:$JAVA\lib\classes.zip
Mac: ./$JAVA:classes/$JAVA:lib:classes.zip

Here . is the current directory and $JAVA is the main Java directory where the different tools like javac were installed.

Posted in: Java

Related FAQ's

Marius Ion ANGEL HOT SOFT LLC (800) 316-7677