עיצוב תוכנה מונחה עצמים

  • Published on
    22-Feb-2016

  • View
    55

  • Download
    0

Embed Size (px)

DESCRIPTION

. 1. Outline. Packages and Paths Jar Files. Packages. Packages are the Java mechanism to prevent name collisions between classes. java.util.List java.awt.List !. Package rules: Each class belongs to a package Package may have sub-packages - PowerPoint PPT Presentation

Transcript

PowerPoint Presentation

1Packages and PathsJar FilesOutlinePackage rules:Each class belongs to a packagePackage may have sub-packagesA package path is dot-separatedfoo.goo.Boo Class Boo, sub-package goo, package fooSub-packages has no special properties or privilegesPackagesPackage declaration

package foo.goo;class Boo {...}Package (cont'd)Importing classes from other packages

import fi.fy.Fum; // Just class Fumimport bar.baz.*; // All classes in bar.bazWhen looking for a class, the JRE looks for a .class file indirectories that match the package path: Sub-package =Sub-directory.The root directory is the classpath.Finding a ClassFind the class foo.goo.Boo in the directories tree+-classpath|-...+-foo|-...+-goo|-X.class|-Y.class|-Boo.classA classpath can contain several rootsFinding a Class (cont'd)Finding foo.goo.Boo in classpath=A:B:CA

+-A +-fi +-fy |-Fum.class |-...B

+-B |-... +-foo |-... +-goo |-X.class |-Y.classC

+-C |-... +-foo |-... +-goo |-W.class |-Z.class |-Boo.classJar files are the Java equivalent to libraries and executables.A jar file is a zip archive that contains:.class filesOther needed resources (images, config files)A manifest file (optional)Source files (optional)Jar FilesThe manifest file is a text file with key: value properties.The JRE reads these properties when it loads the .jar file.Important properties in a jar manifestManifest-Version: For backward compatibility.Main-Class: The class containing the main method to run.Class-Path: Space-separated list of other class paths to use.

Seehttp://java.sun.com/javase/6/docs/technotes/guides/jar/for details on jar and manifest specifications.The Manifest FileManifest.mf

Manifest-Version: 1.0Main-Class: a.b.c.FooClass-Path: mylib1.jar mylib2.jarManifest exampleJar files as Java executablesYou can run a jar file by running:java -jar jar-file(Or double-click on it, in some systems)The Java Runtime will load all the content of the jar, includingits classpath, and will run the main class that is written in themanifest.Using Jar filesJar files as Java shared librariesIncluding a jar file in a classpath will cause the JRE to lookin the archive for classes and resources, as if it was anotherdirectory.Creating a jar file using the command-line tool:

jar cf files . . .Creating Jars

Recommended

View more >