jar and packages

33
JAR and Packages Section 4.5 (JIA’s) Section 3.3 (ALBING’s) Section 5.11 (ALBING’s)

Upload: tayte

Post on 23-Feb-2016

51 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JAR and Packages

JAR and Packages

Section 4.5 (JIA’s)Section 3.3 (ALBING’s)

Section 5.11 (ALBING’s)

Page 2: JAR and Packages

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

Page 3: JAR and Packages

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

Page 4: JAR and Packages

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

Page 5: JAR and Packages

Access Rights for the Different Visibilities in Java

class \ have access to PrivatePackage--

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

http://www.uni-bonn.de/~manfear/javaprotection.php

Page 6: JAR and Packages

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

Page 7: JAR and Packages

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/

Page 8: JAR and Packages

Packages package PackageName

package geometry

public class Point { private double x,y; …

}

This says the class Point belongs to a package called geometry

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

Page 9: JAR and Packages

Add a Class to a Package package edu.csbsju.cs

domain.organization.division.etc… easily create packages that are unique

must have a subdirectory structure that matches the package

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

Page 10: JAR and Packages

Compile and Run Package Classes

PackageTest example /net/people/faculty/cs/irahal/csci230bin/

Packages_JARs/PackageTest

Go to directory containing directory edu javac edu/csbsju/cs/Employee.java

Creates a edu.csbsju.cs.Employee.class

javac edu/csbsju/cs/Employee.java where do compiled classes go? Can that be changed?

javac –d MyDir1/MyDir2… edu/csbsju/cs/Employee.java Whole package structure is recreated

java edu.csbsju.cs.Employee

Page 11: JAR and Packages

Class Importation A class in a package can be accessed by other classes via

the qualified name e.g. geometry.Point

importing the class into the using class (at the top) 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

Page 12: JAR and Packages

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

Page 13: JAR and Packages

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/classes/Java/mysql-connector-java-3.1.10-bin.jar:/usr/people/classes/Java/objectdraw/objectdraw.jar:/usr/people/classes/Java:.

Page 14: JAR and Packages

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

: separated java -cp .:/net/people/faculty/cs/jschnepf/csci230bin/Packages_JARs/

SomeClass Make sure you always include current directory otherwise program won’t

run

How does the search work? Look in imported Java packages

java.lang.Employee (java.lang imported by default) java.lang.edu.csbsju.cs.Employee

Looks in current directory 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

Page 15: JAR and Packages

Package design for Project edu.csbsju.bankdirect.TEAMNAME.frontend

edu.csbsju.bankdirect.TEAMNAME.frontend.inquiry edu.csbsju.bankdirect.TEAMNAME.frontend.billing

Classes common to more than one frontend module go in edu.csbsju.cs.bankdirect.TEAMNAME.frontend

Separate class files (under bin) from source files (under src)

Page 16: JAR and Packages

JAR Files

Page 17: JAR and Packages

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/deployment/jar/

Page 18: JAR and Packages

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

Page 19: JAR and Packages

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

Main-class 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>

Page 20: JAR and Packages

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/tools/windows/jar.html

Page 21: JAR and Packages

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 META-INF/MANIFEST.MF

Page 22: JAR and Packages

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 ….

Page 23: JAR and Packages

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)

Page 24: JAR and Packages

Updating a JAR File

jar uf jar-file input-file(s) jar uf TicTacToe.jar images/new.gif

Adds new.gif to jar file

Page 25: JAR and Packages

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>

Page 26: JAR and Packages

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

Page 27: JAR and Packages

Manifest Jar tool automatically adds a manifest file

to the JAR archive with path name META-INF/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

Page 28: JAR and Packages

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.

Page 29: JAR and Packages

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

Page 30: JAR and Packages

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.0Created-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

Page 31: JAR and Packages

The Manifest Main attributes are the attributes that are

present in the main section of the manifest Manifest-Version:

Defines the manifest file version. The value is a legitimate version number. This attribute is generated by the jar tool.

Created-By: 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.

Page 32: JAR and Packages

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

Page 33: JAR and Packages

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 A file containing the line: Main-Class: edu/csbsju/cs/Employee

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 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