Previous | Next | Trail Map | The Java Development Environment | Managing Your Programming Environment


The CLASSPATH Environment Variable

To run a stand-alone Java application, you specify the name of the Java application that you wish to execute to the Java interpreter. To run an applet, you specify the applet's name with an APP tag within an HTML file. The HotJava browser passes the applet's name to the Java interpreter. In either case, the application or applet that you are running could be anywhere on your system or on the network. Also, the application or applet might use other classes or objects that are in the same or different location.

Because your classes could be anywhere, you must indicate to the Java interpreter where it can find the classes that you are trying to run. You do this with the CLASSPATH environment variable. The CLASSPATH environment variable is comprised of a list of directory names that contain compiled Java classes. The actual construct of CLASSPATH depends on the system you are running.

When the interpreter gets a classname, either from the command line, from the HotJava browser, or from an application or applet, the interpreter searches each directory in the CLASSPATH until it finds the class it's looking for.

You should put the top-level directory that contains your Java classes in your CLASSPATH. By convention, many people have a classes directory in their home directory where they put all of the Java code. If you have such a directory, you should put that directory in your CLASSPATH. However, when you are working with applets, it's convenient to put the applet in a classes directory underneath the directory where the HTML file is that contains the applet. For this and other reasons, it's often convenient to put dot ('.')--the current directory--in the CLASSPATH as well.

The classes included with the Java development environment are automatically available to you because the interpreter automatically appends the correct directory to your CLASSPATH when it starts up.

Order is Important

When the Java interpreter is looking for a class, it searches the directories indicated by your CLASSPATH in order until it finds a class with the correct name. The Java interpreter runs the first class with the correct name that it encounters and does not search the remaining directories. Normally, it's best to give your classes unique names, but if you can't avoid it, make sure that your CLASSPATH searches your classes in the proper order. Keep this in mind when setting up your CLASSPATH and your source code hierarchy.


Previous | Next | Trail Map | The Java Development Environment | Managing Your Programming Environment