Aqua Phoenix
     >>  Lectures >>  Java 6  


6.6 Java Packages

Java packages are similar to libraries, and provide functionality for some topic of interest. is a package that contains classes for many input/output related topics. java.awt provides functionality for GUI building. etc.

When a company or organization is interested in shipping a Java software, the software is usually not distributed as a collection of loose class files, but instead they are bundled in a package. This package usually comes in the form of a JAR file (Java ARchive).

The benefits of a packages and JAR file are:

  • Organization: classes are organized in a tree, grouped by function and purpose
  • JAR files can act as executables: it is possibe to double click on appropriately set jar files to run a Java program
  • JAR files can be included as libraries to extend functionality of Java software, without having to re-implement (the general idea of a library)

6.6.1 Creating

Several steps are necessary to place a Java class into a package structure.

  1. Each Java source file requires an additional line of code as the first line. To place class w3101_3 into package edu.columbia.cs, the following line is required in class w3101_3:

    package edu.columbia.cs;

    public class w3101_3 {
  2. The directory structure in which this class subsides must follow the structure of the class name. Each word separated by a period is a directory:

    + edu
      + columbia
        + cs
    That is, file exists in directory edu/columbia/cs
  3. To compile java files for a package, individual files can be compiled by changing into their respective directories and running javac there.

    To compile java files that depend on other packages in the same tree, then the java file must be compiled from the root directory. For example:

    edu/columbia/cs/ uses class Student from package edu/columbia/seasTo compile, javac should be called from the parent of directory edu, e.g.

    [ahaubold] mycomputer:/home/ahaubold > javac edu/columbia/cs/
    Because of the directory structure and the package names, javac knows to look under edu/columbia/seas for class Student.

  4. Finally, to package a tree of directories and files, the JAR command is used. Its usage is similar to the Unix TAR (Tape ARchive) command. Again, starting from the root directory:

    [ahaubold] mycomputer:/home/ahaubold > jar -cvf edu.jar edu
    This command creates the archive edu.jar from the file and directory tree edu.

    -cvf is a collection of switches that (c)reate (v)erbose (f)ile, where file indivates that the next token is the filename for the archive (edu.jar)

6.6.2 Using

To use the package and execute some Java class inside the package, the java command is still used, but it is necessary to indicate the package name. If w3101_3.class were a runnable class, the following syntax is necessary to run the class:


java -classpath .:edu.jar edu.columbia.cs.w3101_3

java -classpath .;edu.jar edu.columbia.cs.w3101_3
One of the differences between UNIX and Windows is the usage of characters. Under Unix, the colon (:) separates jar files, whereas under Windows, the semi-colon (;) performs this task.

The above command line explicitely includes the package edu.jar in the classpath, and finally calls on class w3101_3 in package edu.columbia.cs.