JAR and Packages

Download Report

Transcript JAR and Packages

JAR and Packages
Section 4.5 (JIA’s)
Section 3.3 (ALBING’s)
Section 5.11 (ALBING’s)
Packages


Java programs are made up of interacting classes
Classes are small cohesive units which MUST belong
to a package


A Java source file may contain an optional package
declaration statement
Why bother?

Group related classes and interfaces together which results
in a better application organization and easier deployment


Separate your work from code libraries provided by others
Or general from specific / or task-oriented classes
Access Modifiers

Public modifier


The first in the access control hierarchy, public modifiers
expose all variables and methods of a class fully to its
surrounding classes. Thus, all variables and methods
declared public are visible to any other Java classes in the
environment and can be modified (variables only) by them.
Protected modifiers

Restrict the visibility and scope of variables and methods.
Variables and methods declared protected in a class are
visible only to those classes that either extend it or that exist
in the same package
Access Modifiers

Default access modifiers --- Package modifier


Now, you might be wondering what will happen if you don’t
specify an access modifier to a variable or method in a class.
What will be the scope of such an entity ? Well, then the
scope of that variable or method in the class will be
restricted to all other classes in the package to which this
class belongs
Private modifiers

As the name implies, variables and methods declared
private are purely meant for internal purposes. They are
not visible even to sub classes. Thus, their scope is very
much restricted to the class in which they are declared
Access Rights for the Different
Visibilities in Java
Private
Package--default
(no modifier)
Protected
Public
own class (Base)
yes
yes
yes
yes
subclass - same package (SubA)
no
yes
yes
yes
class - same package (AnotherA)
no
yes
yes
yes
subclass - another package
(SubB)
no
no
yes
yes
class - another package
(AnotherB)
no
no
no
yes
class \ have access to
http://www.uni-bonn.de/~manfear/javaprotection.php
Why Use Packages?

Partition the name space and prevent name collisions





Class names must be unique
Suppose you’re developing a public tool
no need to worry about name conflicts in same program
Develop a class Point? we already have java.awt.Point!
When placed in different packages, classes with the same name
can be used by the same class without an conflicts (need to be
qualified)



geometry.Point P = new geometry.Point(10.0,20.0)
java.awt.Point P = new java.awt.Point(10.0,20.0)
Or import one and qualify the other
Java Class Library

More than 1900 classes

Standard java packages are divided into


Core packages: prefixed with java
Extension packages: prefixed with javax


Usually not included in the JDK
http://java.sun.com/javase/6/docs/api/
Packages

package PackageName

package geometry

public class Point {



}
This says the class Point belongs to a package called
geometry


private double x,y; …
Must put the name of the package at the top of your source file BEFORE
anything else
If no package declaration is specified then the class is assumed
to reside in an unnamed default package
Add a Class to a Package

package edu.csbsju.cs



 must have a subdirectory structure that matches the
package



domain.organization.division.etc…
easily create packages that are unique
edu/csbsju/cs/
class must be in cs directory
There is absolutely no relationship whatsoever between nested
packages


edu.csbsju.cs and edu.csbsju
java.util and java.util.jar
Compile and Run Package
Classes

PackageTest example


/net/people/faculty/cs/irahal/csci230bin/Packages_JARs/Pac
kageTest
Go to directory containing directory edu

javac edu/csbsju/cs/Employee.java


javac edu/csbsju/cs/Employee.java where do
compiled classes go? Can that be changed?



Creates a edu.csbsju.cs.Employee.class
javac –d MyDir1/MyDir2… edu/csbsju/cs/Employee.java
Whole package structure is recreated
java edu.csbsju.cs.Employee
Class Importation

A class in a package can be accessed by other classes via

the qualified name


importing the class into the using class (at the top)



e.g. geometry.Point
e.g. import geometry.Point or import geometry.*
referred to as Point thereafter
java.lang contains most basics classes required by any java program so it
is imported automatically
How to access package classes?

Place the package in the source directory (i.e. the directory containing the
class referencing classes in the package)




i.e. place edu/csbsju/cs/Employee.class in folder containing
PackageTest.java
Not very convenient
What if classes are to be accessed by a number of other classes?
Place packages inside a special directories



/usr/people/faculty/cs/jschnepf/JavaPackages
/usr/people/classes/Java
Must be in $CLASSPATH



echo $CLASSPATH
A list of directories or JAR files that the compiler looks in for any class it needs
New directories can be added to $CLASSPATH or specified when compiling and
running program
How to access package classes?

OR reset CLASSPATH to include package path
(persists during user session)



setenv CLASSPATH ${CLASSPATH}:additional value
rehash
echo $CLASSPATH

/usr/people/faculty/cs/irahal/JavaPackages:/usr/java/
lib:/usr/lib/pgsql:/usr/share/java:/usr/people/classe
s/Java/mysql-connector-java-3.1.10bin.jar:/usr/people/classes/Java/objectdraw/objectdra
w.jar:/usr/people/classes/Java:.
How to access package classes?

assume edu/csbsju/cs/Employee.class is in directory
net/people/faculty/cs/jschnepf/csci230bin/Packages_JARs/

javac -cp .:/net/people/faculty/cs/jschnepf/csci230bin/Packages_JARs/
SomeClass.java


java -cp .:/net/people/faculty/cs/jschnepf/csci230bin/Packages_JARs/
SomeClass


: separated
Make sure you always include current directory otherwise program won’t run
How does the search work?

Look in imported Java packages



Looks in current directory



java.lang.Employee (java.lang imported by default)
java.lang.edu.csbsju.cs.Employee
Current directory/Employee (because of .)
Current directory/edu/csbsju/cs/Employee
Looks in other directories


/net/people/faculty/cs/irahal/csci230bin/Packages_JARs/Employee
/net/people/faculty/cs/irahal/csci230bin/Packages_JARs
/edu/csbsju/cs/Employee
Package design for Project

edu.csbsju.bankdirect.TEAMNAME.frontend



Classes common to more than one frontend
module go in


edu.csbsju.bankdirect.TEAMNAME.frontend.inquiry
edu.csbsju.bankdirect.TEAMNAME.frontend.billing
edu.csbsju.cs.bankdirect.TEAMNAME.frontend
Separate class files (under bin) from source files
(under src)
JAR Files
Jar Files



The Java™ Archive (JAR) file
enables you to bundle multiple files into a
single archive file.
Typically a JAR file contains


the class files
auxiliary resources associated with applets and
applications.
http://download.oracle.com/javase/tutorial/deplo
yment/jar/
Jar Benefits


Security: can digitally sign the contents
Decreased download time: Applet can be
downloaded to a browser in a single HTTP transaction




no need for opening a new connection for each file.
Compression: can compress your files for efficient
storage.
Package Versioning: A JAR file can hold data about
the files it contains, such as vendor and version
information.
Portability: standard part of the Java API
Jar Basics






Create a JAR file: jar cf jar-file input-file(s)
View the contents of a JAR file:
jar tf jar-file
Extract the contents of a JAR file
jar xf jar-file
Extract specific files from a JAR file jar xf jar-file file(s)
Run an application packaged as a JAR file (requires the Mainclass manifest header) java -jar app.jar
To invoke an applet packaged as a JAR file
<applet code=AppletClassName.class
archive="JarFileName.jar"
width=width height=height>
</applet>
jar utility

jar cvf JARFileName.jar File1 File2 …









c – create a new archive file
t – display table of contents
u – update existing JAR file (replace updated files, add
missing files)
x – extract files
f – Specified the JAR file name as second command-line
argument
v – verbose
m – adds a manifest to the JAR file
0 – stored without compression
http://java.sun.com/javase/6/docs/technotes/to
ols/windows/jar.html
Non-class files can be included


jar cvf TicTacToe.jar TicTacToe.class
audio images
Jar tool automatically adds a manifest file to
the JAR archive with path name METAINF/MANIFEST.MF
Viewing Contents of a JAR File

jar tf jar-file


t – table
jar tf TicTacToe.jar






META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
….
Extracting Contents of a JAR
File



jar xf jar-file [archived-file(s)]
jar xf TicTacToe.jar TicTacToe.class
images/cross.gif
Places images/cross.gif in current directory

(with no specified files, will extract all)
Updating a JAR File


jar uf jar-file input-file(s)
jar uf TicTacToe.jar images/new.gif

Adds new.gif to jar file
Applets Packaged in JAR Files
Original applet:
<applet code=TicTacToe.class
width=120 height=120>
</applet>
Changed to:

<applet code=TicTacToe.class
archive="TicTacToe.jar"
width=120 height=120>
</applet>
JAR Files as Applications


java -jar jar-file
must add a Main-Class header to the JAR file's
manifest. The header takes the form:
Main-Class: classname
Manifest

Jar tool automatically adds a manifest file to
the JAR archive with path name METAINF/MANIFEST.MF




Describes the special features of the JAR file
Called MANIFEST.MF & located in a special folder called
META-INF
A list of key/value pairs with a colon in the middle
Divided into sections
Cutom Manifest

To modify the manifest:


must first prepare a text file containing the
information you wish to add to the manifest.
use the Jar tool's m option to add the information in
your file to the manifest.
Jar with custom Manifest

jar cfm jar-file manifest-addition input-file(s)




c - create a JAR file.
f – output to go to a file (not std out)
m - merge information from an existing file into the
manifest file of the JAR file you're creating.
 jar-file - the JAR file you're creating
 Manifest -addition - name (or path and name) of
text file whose contents you want to add to the
contents of JAR file's manifest.
input-file(s) -space-separated list of one or more files
Adding main class to manifest

jar cfm MyApp.jar Manifest.txt MyPackage/*.class
 creates the JAR file with a manifest with the following
contents:
Manifest-Version: 1.0

Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass
When you run the JAR file with the command, the main method
of MyClass executes:
java -jar MyApp.jar
The Manifest

Main attributes are the attributes that are present in
the main section of the manifest

Manifest-Version:


Created-By:


Defines the manifest file version. The value is a legitimate version
number. This attribute is generated by the jar tool.
Defines the vendor of the java implementation on top of which this
manifest file is generated. This attribute is generated by the jar tool.
Class-Path :

The value of this attribute specifies the relative URLs of the
extensions or libraries that this application or extension needs. URLs
are separated by one or more spaces. The application or extension
class loader uses the value of this attribute to construct its internal
search path.
Example of a Manifest

















Manifest-Version: 1.0
Main-Class: common/Main
Class-Path: lib/
Specification-Title: Java Platform API Specification
Specification-Version: 1.4
Implementation-Title: Java Runtime Environment
Implementation-Version: 1.4.0-rc
Created-By: 1.4.0-rc (Sun Microsystems Inc.)
Implementation-Vendor: Sun Microsystems, Inc.
Specification-Vendor: Sun Microsystems, Inc.
// DON’T FORGET an empty line
Name: common/class1.class
MD5-Digest: (base64 representation of MD5 digest)
// DON’T FORGET an empty line
Name: edu/csbsju/cs/
(the / at the end distinguished a file from a package)
Sealed: True
// DON’T FORGET an empty line
Self-Running JAR files

To package an application


place all files that your application needs into a JAR file
add manifest entry that specifies the main class of your program


CAVEAT




don‘t add .class to the main class name
last line in manifest must end with a newline character
jar cvfm Employee.jar mainclass.mf edu


A file containing the line:
Main-Class: edu/csbsju/cs/Employee
edu directory processed recursively
jar cvfm JARTest.jar ManInf PackageTest.* edu
Users can now run the program as


java –jar Emloyee.jar
Use jar xvf to show Manifest file