Java Packages - PowerPoint PPT Presentation

About This Presentation
Title:

Java Packages

Description:

... is in CreateSequentialFile.java. The classes it uses ... Create a jar files out of all the source files (java) into a prj2.jar. Submit prj2.jar file. ... – PowerPoint PPT presentation

Number of Views:769
Avg rating:3.0/5.0
Slides: 12
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:
Tags: java | packages

less

Transcript and Presenter's Notes

Title: Java Packages


1
Java Packages
  • B.Ramamurthy

2
Topics for Discussion
  • Keyword package
  • Packaging a set of classes
  • CLASSPATH reference to a package
  • Example
  • Creating a Java Archive (jar)
  • Using a jar file
  • Summary

3
Keyword package
  • Package construct is used to package a set of
    related class.
  • You will find many examples in Java API
    java.util package, java.lang package etc.
  • To include a class into a package add a
    statement
  • package package_name
  • before the class header.

4
package Definition
  • 1. Add package package_name to all the java
    source files that contains class definition to be
    included in the package.
  • 2. Create a directory package_name
  • 3. Compile and place the class files in a
    directory package_name

5
package Usage
  • 1. Update your CLASSPATH environment variable to
    include the path where the directory is located.
  • 2. Make sure the update is effective by executing
    source .cshrc
  • 3. Include import package_name. in the
    application/applets where you want to use these
    classes.

6
Example
  • Look at ch17, fig17_04
  • The application is in CreateSequentialFile.java.
  • The classes it uses are BankU.javaI and
    BankAccountRecord.java
  • Lets create a package ch17 out of these two
    classes.
  • Create a directory ch17 anywhere you want. Move
    BankUI.java and BanAccountRecord.java into this
    directory.
  • Add package ch17 at the top of the two files.
    Remove any other reference to packages.

7
Example (contd.)
  • Compile the files BankUI.java and
    BankAccountRecord.java
  • Update .cshrc to include the path to the parent
    directory of ch17.
  • source .cshrc
  • Update CreateSequentialFile.java to import
    classes from ch17
  • import ch17.
  • Compile and execute.

8
Archiving the Package
  • Often once the classes are related by package,
    and after they are tested well, they are
    compressed using a jar tool to provide an easy
    way to transport the package as one.
  • Command (See man jar)
  • jar cf jar_file_name classes to be archived
  • Examples
  • jar ch17.jar BankUI.class Record.class
    BankAccountRecord.class

9
Including jar file in CLASSPATH
  • When a package is in the form of a .jar file, you
    need actually include the jar file in the
    CLASSPATH.
  • Example
  • setenv CLASSPATH ./projects/bina/ch17.jar

10
Jar your files for Prj2
  • Create a jar files out of all the source files
    (java) into a prj2.jar
  • Submit prj2.jar file.
  • One more modification No need for print in prj2.
    We will do that prj3.
  • But save and load are needed. These can be hidden
    operations, that is, no need for an user
    interface for data saving and restoring.

11
Summary
  • You can collect all the useful classes you have
    created into a package.
  • Create a CS2 package.
  • Use it in your future applications.
Write a Comment
User Comments (0)
About PowerShow.com