Deploying Java applications as JAR files - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Deploying Java applications as JAR files

Description:

A JAR file is essentially a ZIP file with specific contents: ... To run the application bundled within a JAR file, issue the following command: ... – PowerPoint PPT presentation

Number of Views:307
Avg rating:3.0/5.0
Slides: 20
Provided by: drmarkh
Category:

less

Transcript and Presenter's Notes

Title: Deploying Java applications as JAR files


1
Deploying Java applications as JAR files
2
Consider the following project
  • This application consists of three files in two
    directories, in packages called se2030.jigloodemo
    and se2030.jigloodemo.hornick

3
When you run an application from within Eclipse,
it issues the following command
  • java cp D\My Documents\MSOE\Courses\Example
    Programs\se2030\JiglooDemo\bin
    se2030.jigloodemo/JiglooDemoApp
  • java is the command that runs the Java Virtual
    Machine
  • Same as C\Program Files\Java\jdk1.6.0_03\bin\java
    .exe
  • JiglooDemoApp is the name of the main class
  • se2030.jigloodemo is the name of the package
    containing the main class (se2030.jigloodemo.horni
    ck is the name of the package containing the ui
    class)
  • -cp ltclasspathgt specifies the base directory
    where the .class file(s) for the packages are
    located
  • Not including the package subdirectories

4
The formal syntax isjava cp ltpathgt ltpackagegt /
ltmain_classgt
  • javaw is another command that runs the Java
    Virtual Machine
  • without a console window
  • ltmain_classgt is the name of the main class
  • The one containing the main() method
  • ltpackagegt is the name of the package containing
    the main class
  • Note the . separator within the package
    specification
  • Note the / separator after the package
    specification
  • -cp ltclasspathgt specifies the classpath
  • the base directory containing the classes that
    make up the application
  • If the classes are distributed among more than
    one base directory, then ltdirgt is a
    semicolon-separated list of directory paths

5
You can create a shortcut to run the application
from the Desktop
In Windows 7 1. Right-click the Desktop and
select New/Shortcut from the context menu that
appears. 2. Browse to the place on your file
system containing the Java VM, as shown (your
location may be different).
6
Creating a shortcut, continued...
3. Type the name for your shortcut that will
appear beneath the shortcut icon on your desktop.
7
Creating a shortcut, continued...
  • Select Properties of the resulting shortcut icon
    on your desktop. The dialog to the left appears.
  • Append the file path to the class file to the
    existing Target, omitting .class.
  • If it consists of only a single directory, the
    classpath can be specified in the Start in
    text box, and the -cp option is not needed in
    the Target specification

8
Next, consider the following project
This application consists of two source files in
two different classpaths (src and auxiliary), but
the same package (edu.msoe.se2030.demo)
9
Using the preceding project as an example
  • java cp D\My Documents\MSOE\Courses\Example
    Programs\se2030\JARDemo\src D\My
    Documents\MSOE\Courses\Example Programs\se2030\JAR
    Demo\auxiliary edu.msoe.se2030.demo/JARDemoApp
  • JARDemoApp is the name of the main class
  • edu.msoe.se2030.demo is the name of the package
    containing the main class
  • The classpath must specify both base directories
    where the .class file(s) are located (because
    they differ)
  • Separated by a semicolon

10
Finally, consider the following project
This application consists of one source file, but
the WinplotterDemo project uses an external
library (winPlotter.jar) containing several
user-written classes
11
Using this last project as an example
  • java cp D\My Documents\MSOE\Courses\Example
    Programs\se2030\WinplotterDemo D\My
    Documents\MSOE\Courses\Example Programs\jars\winPl
    otter.jar edu.msoe.se2030.plot/WinplotterDemoApp
  • WinplotterDemoApp is the name of the main class
  • edu.msoe.se2030.plot is the name of the package
    containing the main class
  • The classpath specifies both the directory where
    the WinplotterDemoApp.class file is located
  • As well as the path to the winPlotter.jar file
    that contains the external user-written classes

12
A Java Archive (JAR) file enables you to bundle
multiple files into a single archive file
  • A JAR file is essentially a ZIP file with
    specific contents
  • The files you want to zip into the file
  • .class files
  • Source files (.java) if you want to enable
    debugging
  • Javadoc files if you want to provide
    context-sensitive help for the classes in the JAR
    file
  • A manifest file (MANIFEST.MF)
  • Which specifies whats in the JAR file

13
The jar utility is used to create JAR files
  • jar cfm ltjarfilegt ltmanifestgt ltfilesgt
  • jar is the command that runs the jar utility
  • Same as C\Program Files\Java\jdk1.6.0_03\bin\jar.
    exe
  • jarfile is the name of the JAR file you want to
    create
  • manifest is the name of a file containing
    manifest information
  • Note  The contents of the manifest must be
    encoded in ansi.
  • files specifies the files you want to place in
    the JAR file
  • Separated by spaces

14
To bundle files in the same directory into a JAR
file
  • jar cfm MyApp.jar manifest.txt edu/msoe/se2030/dem
    o/JARDemoApp.class edu/msoe/se2030/demo/DemoDispla
    yer.class
  • This assumes
  • you are issuing the jar command from within the
    directory of D\My Documents\se2030\JARDemo\bin
  • manifest.txt is in the same bin directory
  • edu/msoe/se2030/demo is the name of the package
    such that all class files are located in D\My
    Documents\JARDemo\bin\edu\msoe\se2030\demo
  • the capitalization of the class filename is
    identical to the capitalization of the class
    itself (be careful on Windows because the OS is
    case-insensitive but Java is NOT)

15
Manifest details
  • Manifest.txt is an ansi text file (created with
    Notepad or similar text editor) containing the
    following text
  • Manifest-Version 1.0
  • Created-By 1.6.0_03-b05 (Sun Microsystems Inc.)
  • Main-Class edu.msoe.se2030.demo.JARDemoApp

16
The case where .class files are in separate
directories and/or separate packages
  • jar cfm MyApp.jar manifest.txt
  • C ./bin1 edu/msoe/se2030/demo/JARDemoApp.class
    C ./bin2 edu/msoe/se2030/auxiliary/DemoDisplayer
    .classThis assumes
  • you are issuing the jar command from within a
    project directory such as D\My
    Documents\JARDemo and bin1 and bin2 are
    subfolders
  • manifest.txt is in the same project directory
  • edu/msoe/se2030/demo and edu/msoe/se2030/auxiliary
    are the names of the packages such that the
    class files are located in D\My
    Documents\JARDemo \bin1\edu\msoe\se2030\demo and
    D\My Documents\JARDemo \bin2\edu\msoe\se2030\auxi
    liary

17
If your JAR file references other JAR files (like
WinPlotter.jar)
  • The Manifest.txt must contain a reference to the
    location of the other JAR file(s)
  • Manifest-Version 1.0
  • Created-By 1.6.0_03-b05 (Sun Microsystems Inc.)
  • Main-Class edu.msoe.se2030.demo.JARDemoApp
  • Class-Path ./winPlotter.jar

18
To deploy your application, you just have to copy
the JAR file to someplace (e.g. C\temp) on the
target PC
  • To run the application bundled within a JAR file,
    issue the following command
  • java jar C\temp\MyApp.jar
  • Or create a shortcut containing the above command

19
Online tutorial
  • Packaging Programs in JAR files
  • http//java.sun.com/docs/books/tutorial/deployment
    /jar/index.html
Write a Comment
User Comments (0)
About PowerShow.com