JAVA PACKAGES


Contents


Overview

Examples

Assume that you are working in a directory called Javadir, and that you create four files, whose contents are shown below.

file 1
package ListPkg;
public class List { ... }
class ListNode {...}

file 2
package ListPkg;
public class NoNextItemException { ... }


file 3
public class Test { ... }
class Utils { ... }

file 4
class Test2 { ... }
Here are the directories and file names you must use:

And here are the classes that can be accessed by the code in each file:

Here's a summary of the example:
File Contents Directory/FileName Can Access
package ListPkg;
  public class List {...}
  class ListNode {...}
ListPkg/List.java List, ListNode, NoNextItemException
package ListPkg;
  public class NoNextItemException
  {...}
ListPkg/NoNextItemException.java List, ListNode, NoNextItemException
public class Test {...}
class Utils {...}
Test.java ListPkg.List, ListPkg.NoNextItemException, Test, Utils, Test2
class Test2 {...}
any-name.java ListPkg.List, ListPkg.NoNextItemException, Test, Utils, Test2

How the Java Compiler Finds Files

When you compile a file that uses a class (or interface) that is not defined in the same file, the Java compiler uses

to try to locate the class definition. For example, assume that you are working in directory Javadir, which contains one file named Test.java: Since List is not defined in Test.Java, and since there is no file List.java in the current directory, the compiler will look for List.java in the ListPkg subdirectory (since Test.java imports the ListPkg package).

Now suppose that the ListPkg subdirectory contains two files: List.java and ListNode.java, both part of the ListPkg package. Also assume that List.java uses the ListNode class defined in ListNode.java. If you try to compile just List.java in the ListPkg subdirectory, you will get an error, because the compiler will try to find the file ListNode.java in a "ListPkg" subdirectory of the current directory, rather than looking in the current directory itself.

There are (at least) three ways to solve this problem:

  1. Always compile a package from the parent directory. For example, compile List.java from Javadir, rather than from Javadir/ListPkg; in the Javadir directory, type:
  2. Always compile all files in a package at the same time; for example, in the directory Javadir/ListPkg type:
  3. Make a circular link from the package subdirectory to itself; for example, in the directory Javadir/ListPkg type:

The CLASSPATH Environment Variable

To use a package that is not in a subdirectory of the current directory (i.e., the directory in which you invoke javac), you must set the environment variable CLASSPATH to tell the java compiler where to look.

For example, if there were a List package in /p/course/cs368-horwitz/public/ListPkg, you would set CLASSPATH like this:

  setenv CLASSPATH .:/p/course/cs368-horwitz/public
Including the dot and the colon before the directory tells the compiler also to look in the directory in which the compile is being done. Note that you should set the CLASSPATH variable to the parent of the "ListPkg" subdirectory, not to the ListPkg subdirectory itself.