Software Design (CS223 2004F)

Java Packages

Summary: This document gives a quick overview of the purpose of Java packages and how to work with them in the MathLAN.

Contents:

Introduction

Because Java was designed for programming in the large, the designers of Java worried about name clashes. (E.g., If I design a class called Foo and you design a class called Foo, how do we distinguish them, particularly in a program that uses both?) As a solution to this problem, they created packages. In essence, each package creates its own name space. (Alternatively, packages give you longer names for things.)

You've used packages many times in programming with Java. For example, when you use the java.util.Vector class, you're using a class in package java.util.

Although most programmmers have no trouble using classes in already-created packages, many programmmers are not sure how to manage their own packages. This document attempts to explain how to use packages in the MathLAN (and, I expect, on most *nix systems).

Package Names

At first glance, packages are easy. You choose a name for your package and you put the keyword package and the name of the package at the start of your source file. (You should have seen these details in the some examples.)

However, choosing the name for a package can be hard. Different developers sometimes seem to have different models for naming their packages. The standard suggests that you use a fully qualified name that is, essentially, the reverse of the domain you might use. For example, my code should probably be in a package that starts with edu.grinnell.cs.rebelsky.

For this class, I would prefer that you use cs223.group-or-individual-nname.assignment. For example, Daren's second homework might be in package cs223.brantley.hw2. Similar, if a team called blue works together on homework 4, they should put their work in a package called cs223.groupblue.hw4.

Directories and Package Names

The next thing you need to realize is that package names must correspond to directory names. Hence, if you have a package named project (not a good name for a package), it must be in a directory named project. If you have a package named cs223.groupblue.hw4, it must be in a a directory named hw4, which is in a directory named groupblue, which is in a directory named cs223.

Compiling

There's nothing special about compiling files in packages. Just use the name of the file.

Running

The hardest part of using packages seems to be running the class with the main. Using packages you've created yourself also seems to be problematic. The important thing you need to know is that the Java interpreter needs to know where to look for your packages. You tell the interpreter that by setting the CLASSPATH environment variable, giving it the parent directory of your packages. (Note that you can have multiple directories in your CLASSPATH.) I recommend adding something like the following to the end of your .bashrc:

export CLASSPATH="$CLASSPATH:/home/yourusername/directoryforthisclass:.:/home/rebelsky/Web/Courses/CS223/2004F/Examples"

Also note that you should give the fully-qualified name, such as

# java cs223.brantley.hw2.StartHere

Using Your Packages

Using packages requires the same solution as for running classes: Make sure that your CLASSPATH is set correctly.

 

History

Tuesday, 31 August 2004 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Tue Dec 7 21:49:34 2004.
The source to the document was last modified on Tue Aug 31 21:50:00 2004.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS223/2004F/Handouts/packages.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu