Lab 5: Platforms, Packages, and Archives
Glossary
Abstract Platform
The term platform (also called a physical platform or a runtime environment) is commonly used to collectively refer to a computer together with its operating system. An abstract platform (also called a virtual platform) is a document that specifies the services provided by a platform without describing a physical platform that provides these services. This is done by describing the services purely in terms of the interfaces they present to application programmers (APIs). An application developed using an abstract platform can run on any physical platform implementation:
There are currently three abstract Java platforms: the Java 2 Enterprise Edition (J2EE), the Java 2 Standard Edition (J2SE), and the Java 2 Micro Edition (J2ME). Generally speaking, J2EE describes an abstract server platform, J2SE describes an abstract client platform, and J2ME describes an abstract PDA (Personal Digital Assistant) platform.
Application Programmer Interface (API)
An API describes a set of related services provided by an abstract platform. Typically, these services might be described by a package of class specifications and interfaces. Common APIs include:
A package is a named collection of classes and sub-packages. A package defines a namespace. Thus, a program might consist of two classes named Star, as long as they were contained in two different packages.
The J2SE organizes its class library into three packages:
A runtime environment (also called a physical platform) implements an abstract platform specification. A runtime environment is a combination of hardware and software. The Java 2 Runtime Environment (J2RE) is Sun's implementation of the J2SE abstract platform. The J2RE contains Java's pre-defined classes and the Java virtual machine.
Software Development Kit
A software development kit (SDK) is a suite of libraries and tools (compilers, etc.) for developing applications. The Java 2 SDK (J2SDK) is Sun's SDK for developing applications that will run on the J2SE abstract platform. It includes the Java 2 Runtime Environment as well a javac, javadoc, applet viewer, etc. The J2SDK is also called the Java Development Kit (JDK).
Creating Packages
We have seen that the Java platform specification organizes its class library into packages and sub-packages. Programmers can organize their class libraries in the same way. For example, assume the files Apple.java, Banana.java, and Peach.java are to contain the definitions of the Java classes Apple, Banana, and Peach, respectively. Instances of these classes will represent virtual trees, so we want to place them in a special package named tree.
If we have decided to keep all of our packages in a directory called
c:\jlab,
then we create a subdirectory of c:\jlab called
tree, and
place all three Java files in this directory. At the top of each file place
the Java statement:
We're not done! Before programmers can use these files, the Java compiler and virtual machine must know where to look for them. Starting with Java version 1.2, the Windows implementations of the compiler and virtual machine consult the PATH variable in the DOS command shell environment. Use the Environment tab of the System control panel to append the directories c:\jlab\tree and c:\jlab\food to the PATH variable.
A class in another package can now refer to virtual trees and fruits by qualifying names with
When a Java file doesn't declare which package it belongs to, then we say that it belongs to the default package.
Package Visibility
Every field and method in a class declaration declares its visibility: public (visible to clients, extensions, and local methods ), protected (visible to extensions and local methods), or private (visible to local methods only). But what happens if a programmer forgets to declare the visibility of a field or method? The default visibility of a field or method is package visibility: visible to local methods and all clients and extensions that are in the same package.
Hidden Classes
Suppose the file c:\jlab\tree\Apple.java also contains the declaration of a class called Redwood:
Problem
Create a package called molecule that contains classes named Water, Sugar, Carbon Dioxide, and SulfuricAcid, and a sub-package named molecule.atom. The atom sub-package contains classes named Carbon, Hydrogen, Oxygen, and Sulfur. Each class in the molecule package should re-define the toString() method to print the numbers and types of atoms that constitute the molecule. For example, molecule.Sugar.toString() should return the string:
Create a class called ChemTest belonging to the default package. ChemTest.main() creates and prints instances of each molecule and atom, but does not contain any import statements.
Archives
A file compression program can reduce the size of a file. For example, a compression program might replace the text:
An archive is a single file that contains several compressed files appended together. Programs like zip and tar can be used to create archives and to extract and decompress the files in an archive. The Java SDK comes with an archive creating/extracting tool called jar. The Java on-line tutorial has a chapter on using the jar tool located at:
The cvf option of the jar tool is used to verbosely create an archive file:
The tf option is used to display the contents of an archive:
The xf option is used to extract and decompress an archive:
The java virtual machine can execute an archive if the archives manifest (i.e. table of contents) contains a line specifying which class in the archive is the main class. In our banking example ATM is the main class.
Create a file called mainclass.txt containing the single line:
Create a new jar file with the contents of mainclass.txt added to the manifest:
Create an archive called chem.jar containing all of the ChemTest.java, ChemTest.class, and all of the files in the molecule and atom packages. Compare the size of the archive with the total sizes of all of the decompressed files it contains. How percentage of size reduction was acheived?