CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Packages

Packages
Packages
Make classes

Making the classes of a package available to other classes

  • When you compile a class that contains a package statement, the class becomes part of the package and classes outside the package can't access it.
  • Once you make a package available to an application, you can code import statements within the classes of the application to import the necessary classes of the package.

How to make a package available to the classes in a single application

  • Copy the directories and files of the package to the root directory of the application.

How to make a package available to the classes in any application

  1. Create a JAR file for the subdirectories and files of the package.
  2. Move the JAR file to the SDK's \jre\lib\ext directory.

Syntax for creating a JAR file for a package

c:\parentDir>jar cvf JARFilename.jar packagePath/*[.class]

A statement that creates a JAR file for the murach packages

C:\java\lineItem>jar cvf murach.jar murach/*

Resulting output

added manifest
adding: murach/business/(in = 0) (out= 0)(stored 0%)
adding: murach/business/LineItem.class(in = 1226) (out= 643)(deflated 47%)
adding: murach/business/LineItem.java(in = 1078) (out= 346)(deflated 67%)
adding: murach/business/Product.class(in = 1084) (out= 549)(deflated 49%)
adding: murach/business/Product.java(in = 940) (out= 308)(deflated 67%)
adding: murach/database/(in = 0) (out= 0)(stored 0%)
adding: murach/database/ProductDB.class(in = 829) (out= 541)(deflated 34%)
adding: murach/database/ProductDB.java(in = 1175) (out= 413)(deflated 64%)
adding: murach/presentation/(in = 0) (out= 0)(stored 0%)
adding: murach/presentation/Validator.class(in = 2057) (out= 1072)(deflated 47%)
adding: murach/presentation/Validator.java(in = 2357) (out= 571)(deflated 75%)
The statements for importing the classes in the packages
import murach.business.*;
import murach.database.*;
import murach.presentation.*;

A statement that excludes the source files from a JAR file

C:\java\lineItem>jar cvf murach.business.jar murach/business/*.class

Note

  • If you don't want to include the source files for a package in a JAR file, you can code the .class extension in the file specification on the jar command. Then, only the class files will be included.
Previous | Introduction | Store classes | Compile classes | Make classes | Next