Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "JAR and Packages Section 4.5 (JIA’s) Section 3.3 (ALBING’s) Section 5.11 (ALBING’s)"— Presentation transcript:

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

2 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

3 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

4 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

5 Access Rights for the Different Visibilities in Java class \ have access toPrivate Package--default (no modifier) ProtectedPublic own class (Base)Baseyes subclass - same package (SubA)SubAnoyes class - same package (AnotherA)AnotherAnoyes subclass - another package (SubB)SubB no yes class - another package (AnotherB)AnotherB no yes http://www.uni-bonn.de/~manfear/javaprotection.php

6 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

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

8 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

9 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

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

11 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

12 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

13 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.10- bin.jar:/usr/people/classes/Java/objectdraw/objectdra w.jar:/usr/people/classes/Java:.

14 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

15 Package design for Project edu.csbsju.bankdirect.TEAMNAME.frontend edu.csbsju. bankd irect.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)

16 JAR Files

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

18 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

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

20 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 http://java.sun.com/javase/6/docs/technotes/to ols/windows/jar.html

21 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

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

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

24 Updating a JAR File jar uf jar-file input-file(s) jar uf TicTacToe.jar images/new.gif Adds new.gif to jar file

25 Applets Packaged in JAR Files Original applet: <applet code=TicTacToe.class width=120 height=120> Changed to: <applet code=TicTacToe.class archive="TicTacToe.jar" width=120 height=120>

26 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

27 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

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

29 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

30 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

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

32 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

33 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


Download ppt "JAR and Packages Section 4.5 (JIA’s) Section 3.3 (ALBING’s) Section 5.11 (ALBING’s)"

Similar presentations


Ads by Google