Java classes, packages, and directories

Each Java™ class is part of a package. The first statement in a Java source file indicates which class is in what package. If the source file does not contain a package statement, the class is part of an unnamed default package.

The package name relates to the directory structure in which the class resides. The integrated file system supports Java classes in a hierarchical file structure that is similar to what you find on most PC and UNIX systems. You must store a Java class in a directory with a relative directory path that matches the package name for that class. For example, consider the following Java class:

     package classes.geometry;
     import java.awt.Dimension;

     public class Shape {

     Dimension metrics;

        // The implementation for the Shape class would be coded here ...

     }

The package statement in the previous code indicates that the Shape class is part of the classes.geometry package. For the Java runtime to find the Shape class, store the Shape class in the relative directory structure classes/geometry.

Note: The package name corresponds to the relative directory name in which the class resides. The Java virtual machine class loader finds the class by appending the relative path name to each directory that you specify in the classpath. The Java virtual machine class loader can also find the class by searching the ZIP files or JAR files that you specify in the classpath.

For example, when you store the Shape class in the /Product/classes/geometry directory in the "root" (/) file system, you need to specify /Product in the classpath.

Figure 1: Example directory structure for Java classes of the same name in different packages

This graphic shows an example directory structure for Java classes of the same name in different packages.

Note: Multiple versions of the Shape class can exist in the directory structure. To use the Beta version of the Shape class, place /Beta/myclasses in the classpath before any other directories or ZIP files that contain the Shape class.

The Java compiler uses the Java classpath, package name, and directory structure to find packages and classes when compiling Java source code. For more information, see Java classpath.