1 / 11

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

עיצוב תוכנה מונחה עצמים. תירגול 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

tilden
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. עיצוב תוכנה מונחה עצמים תירגול 1

  2. Outline Packages and Paths Jar Files

  3. Packages Packages are the Java mechanism to prevent name collisions between classes. java.util.Listjava.awt.List! Package rules: Each class belongs to a package Package may have sub-packages A package path is dot-separated foo.goo.Boo Class Boo, sub-package goo, package foo Sub-packages has no special properties or privileges

  4. Package (cont'd) Importing classes from other packages import fi.fy.Fum; // Just class Fum import bar.baz.*; // All classes in bar.baz Package declaration package foo.goo; class Boo {...}

  5. Finding a Class Find the class foo.goo.Boo in the directories tree +-classpath |-... +-foo |-... +-goo |-X.class |-Y.class |-Boo.class When looking for a class, the JRE looks for a .class file in directories that match the package path: Sub-package = Sub-directory. The root directory is the classpath.

  6. Finding a Class (cont'd) Finding foo.goo.Boo in classpath=A:B:C A +-A +-fi +-fy |-Fum.class |-... B +-B |-... +-foo |-... +-goo |-X.class |-Y.class C +-C |-... +-foo |-... +-goo |-W.class |-Z.class |-Boo.class A classpath can contain several roots

  7. Jar Files Jar files are the Java equivalent to libraries and executables. A jar file is a zip archive that contains: .classfiles Other needed resources (images, config files) A manifestfile (optional) Source files (optional)

  8. The Manifest File The 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 manifest Manifest-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. See http://java.sun.com/javase/6/docs/technotes/guides/jar/ for details on jar and manifest specifications.

  9. Manifest example Manifest.mf Manifest-Version: 1.0 Main-Class: a.b.c.Foo Class-Path: mylib1.jar mylib2.jar

  10. Using Jar files Jar files as Java shared libraries Including a jar file in a classpath will cause the JRE to look in the archive for classes and resources, as if it was another directory. Jar files as Java executables You 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, including its classpath, and will run the main class that is written in the manifest.

  11. Creating Jars Creating a jar file using the command-line tool: jar cf <jar-file> <manifest-file> files . . .

More Related