Skip to content

Packages and the Classpath

Estimated time to read: 3 minutes

The Java directory system

All operating systems define some way to group files into a directory.

Windows uses expressions like: C:\Program Files\IBM\SPD60\eclipse.exe

Unix uses: /home/jsmith/IBM/SDP60/eclipse

Java uses a period to separate directories and call these structures packages: io.entityfour.utilityclasses

Java finds the packages and classes within them at runtime, following the structure above.

Packages are portable

  • Java programs are often moved from one OS to another.
  • They must remain 'detached' from the OS's file system

  • Java treats all packages as relative

  • A package systems is not tied to the root of any file system

  • When you move a Java system to a new machine, any root directory can be used to specify the location of the runtime classes.

  • A CLASSPATH system environment variable points to the starting point(s) of the package structure
  • The JVM will resolve runtime class references within the package structure and the CLASSPATH

Example: Moving packages from Windows to Linux

Windows   C:       \My Programs            \Java              \Source               \io                 \entityfour                   \utility                   \gui                   \database Unix   /       \home            \tbooth              \java               \src                 \io                   \entityfour                     \utility                     \gui                    \database

Java packages start at the com directory, meaning: Window's classpath would be set to C:\Program Files\Java\Source\ Unix's classpath would be set to /home/tbooth/java/src/

Packages and the Classpath

It is the classpath that points towards the source directories under which you would find the TLD paths.

Java can use the CLASSPATH variable as a way to find the packages that it will require stored on the local machine.

The CLASSPATH can be set up as a system-wide environment variable or passed to the Java program when it is run.

Example

The latter can be done via: java.exe -classpath C:\JavaCode\Project1 io.entityfour.guiclasses.MyProgram

The command above will attempt to run the main() method of the MyProgram class.

When this command is issued, java.exe runs and then looks for MyProgram in the directory C:\JavaCode\Project1\io\entityfour\guiclasses