1 / 9

Chapter 55

Chapter 55. How to Construct JAR files for Program Distribution. The Java JAR file has an unfortunate name, inasmuch as archiving is but one of several reasons why you should gather together all your application files into a JAR file. Other reasons include the following:

liza
Download Presentation

Chapter 55

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. Chapter 55 How to Construct JAR files for Program Distribution

  2. The Java JAR file has an unfortunate name, inasmuch as archiving is but one of several reasons why you should gather together all your application files into a JAR file. Other reasons include the following: • JAR files simplify program distribution. You have only one file to move about, rather than a whole directory, or directory hierarchy, full of files. • JAR files save space and time. You have only one compressed JAR file to store or to ship over the web. • JAR files provide version management. Each time you reach a stable point in your program, you can create a JAR file. • JAR files enable security. You can add a digital signature to a JAR file that lessens the chance that a manipulated version will get into the hands of an innocent user.

  3. Before you create a JAR file, you need to create an application-describing manifest file. Minimally, your manifest file needs just one line that identifies the application class that starts your application. For example, if theMovieApplicationTestor class starts your application, then the manifest file must contain the following line: Main-Class: MovieApplicationTestor

  4. Once you have a manifest file, say manifest.txt, then you can create a JAR file using the command-line jar program. Suppose, for example, that your current directory contains all the class files needed by your movie application program. Then, you can create a JAR file by typing the following: *-- Command | *-- Manifest file | | *-- JAR file produced | | | *-- Included class files | | | | v v v v jar -cmf manifest.txt MovieApplication.jar *.class --- ^ *-- c means create a new JAR file m means use the following manifest file f means use the file name supplied • You can use wildcards to identify a collection of files, as shown. Alternatively, you can supply a list of explicit file names, separated by spaces.

  5. If you have divided your application into packages—such as the application, model, and view packages—then your directory structure will include a directory with application, model, and view subdirectories. To combine all the class files in those subdirectories into a JAR file, you type the following, once you have made the current directory be the parent directory of the application, model, and view directories. jar -cmf manifest.txt MovieApplication.jar application model view • If you want only the class files in those directories, you use a wildcard—by typing, for example, application/*.class, instead of application.

  6. If your application includes all the images in an image directory, you can include those images in the JAR file as follows: jar -cmf manifest.txt MovieApplication.jar application model view images • Alternatively, if your applications includes all the images in the current directory, in files with a jpg extension, you type the following: jar -cmf manifest.txt MovieApplication.jar application model view *.jpg

  7. Once you have a JAR file, running a standalone application is straightforward. You are accustomed to typing the following to start an application from a class file: java MovieApplicationTestor • To start an application from a JAR file, you type the following: java -jar MovieApplication.jar • Note that, for the JAR version to work, you must have created the JAR file using a manifest file, that identifies the MovieApplicationTestor class as the application-starting class.

  8. Certain applications use previously prepared JAR files. If your application uses such a previously prepared JAR file—say, the Entertainment.jar file—you would include a reference to it file in your JAR file's manifest, as shown in the following expansion of the manifest file introduced previously. Main-Class: MovieApplicationTestor Class-Path: Entertainment.jar

  9. If you like, you can include useful descriptive information in the manifest file. For example, you can describe each package by a section that begins with its relative path name, including the trailing slash. Then, you can specify, for example, version details: Main-Class: MovieApplicationTestor Class-Path: Entertainment.jar Name: application/ Specification-Title: "Application specifications" Specification-Version: "3.141" Specification-Vendor: "XYZ, Inc." Implementation-Title: "Application implementation" Implementation-Version: "Build 2.718" Implementation-Vendor: "XYZ, Inc." Name: model ... Name: view ...

More Related