How Java uses Packages and the CLASSPATH variable

The Usual Suspects

There is a great scene at the end of the movie Casablanca. Humphrey Bogart has just killed the German Commander in front of the Chief of Police. The Chief then calls his office and informs them that the Commander has been murdered, and that they should round up the usual suspects. For Java, the CLASSPATH variable is a list of the usual suspects. When Java wants to find a class file, it looks in all the directories that are listed in the CLASSPATH*. In order to have Java look in new places, just add more paths to the CLASSPATH variable.

* There are also system paths that are searched that are not listed in the CLASSPATH.

What is a package?

The simplest definition of a package is a folder that contains java class files. However, packages do more than that. They also indicate where a java class can be found. Essentially, packages allow for an extension to the CLASSPATH list, without adding new paths to it. If a class file is in a package named jbond007, then Java will look in all the paths in the CLASSPATH with jbond007 appended to them.

For example, suppose the CLASSPATH contains

Java will then look for the class file in

As a more complicated example, suppose the package is fiu.cgs4854.hw, then Java will look for the class file in