Deploying Java applications as JAR files

16
Deploying Java applications as JAR files SE-2030 Dr. Rob Hasker 1 Based on material by Dr. Mark L. Hornick

description

Deploying Java applications as JAR files. Based on material by Dr. Mark L. Hornick. Consider the following project. This application consists of two source files in the same directory, in a package called modalDialog. - PowerPoint PPT Presentation

Transcript of Deploying Java applications as JAR files

Page 1: Deploying Java applications as JAR files

Deploying Java applications as JAR files

SE-2030Dr. Rob Hasker

1

Based on material by Dr. Mark L. Hornick

Page 2: Deploying Java applications as JAR files

Consider the following project

SE-2030Dr. Rob Hasker

2

This application consists of two source files in the same directory, in a package called modalDialog

Page 3: Deploying Java applications as JAR files

When you run an application from within

Eclipse, it issues the following command:java –cp “D:\My Documents\MSOE\Courses\Example

Programs\se2030\Multithreading” modalDialog/DialogApp

java is the command that runs the Java Virtual Machine Same as C:\Program Files\Java\jdk1.7.0_40\bin\java.exe

DialogApp is the name of the main class

modalDialog is the name of the package containing the main class

-cp <classpath> specifies the directory where the .class file(s) are located

Not including the package

SE-2030Dr. Rob Hasker

3

Page 4: Deploying Java applications as JAR files

The formal syntax is:java –cp <path> <package> / <main_class>

javaw is another command that runs the Java Virtual Machine without a console window

<main_class> is the name of the main class The one containing the main() method

<package> is the name of the package containing the main class Note the “/” separator after the package specification

-cp <classpath> specifies the classpath the directory containing the classes that make up the application If the classes are distributed among more than one directory, then <dir>

is a semicolon-separated list of directory paths

SE-2030Dr. Rob Hasker

4

Page 5: Deploying Java applications as JAR files

You can create a shortcut to run the application from the Desktop

SE-2030Dr. Rob Hasker

5

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

Page 6: Deploying Java applications as JAR files

Creating a shortcut, continued...

SE-2030Dr. Rob Hasker

6

3. Type the name for your shortcut that will appear beneath the shortcut icon on your desktop.

Page 7: Deploying Java applications as JAR files

Creating a shortcut, continued...

SE-2030Dr. Rob Hasker

7

4. Select Properties of the resulting shortcut icon on your desktop. The dialog to the left appears.

5. Append the file path to the class file to the existing Target.

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

Page 8: Deploying Java applications as JAR files

Next, consider the following project

SE-2030Dr. Rob Hasker

8

This application consists of two source files in two different classpaths (src and auxiliary), but the same package (edu.msoe.se2030.demo)

Page 9: Deploying Java applications as JAR files

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\JARDemo\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 specifies both directories where the .class file(s) are located Separated by a semicolon

SE-2030Dr. Rob Hasker

9

Page 10: Deploying Java applications as JAR files

Finally, consider the following project

SE-2030Dr. Rob Hasker

10

This application consists of one source file, but the WinplotterDemo project uses an external library (winPlotter.jar) containing several user-written classes

Page 11: Deploying Java applications as JAR files

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\winPlotter.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 classesSE-2030

Dr. Rob Hasker11

Page 12: Deploying Java applications as JAR files

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 what’s in the JAR file

SE-2030Dr. Rob Hasker

12

Page 13: Deploying Java applications as JAR files

The jar utility is used to create JAR files

jar cfm <jarfile> <manifest> <files>

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

SE-2030Dr. Rob Hasker

13

Page 14: Deploying Java applications as JAR files

To bundle files into a JAR file:

jar cfm MyDialog.jar manifest.txt modalDialog/DialogApp.class modalDialog/MyDialog.class

(assuming you are issuing the jar command from within the project directory of D:\My Documents\MSOE\Courses\Example Programs\se2030\Multithreading)

manifest.txt is a text file (created with Notepad) containing the following text:

Manifest-Version: 1.0Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.)Main-Class: modalDialog.DialogApp

SE-2030Dr. Rob Hasker

14

Page 15: Deploying Java applications as JAR files

To deploy your application, you just have to copy the JAR file to someplace on the target PC

To run the application bundled within a JAR file, issue the following command:

java –jar “D:\My Documents\MSOE\Courses\Example Programs\se2030\Multithreading MyDialog.jar”

Or create a shortcut containing the above command

SE-2030Dr. Rob Hasker

15

Page 16: Deploying Java applications as JAR files

Online tutorial

“Packaging Programs in JAR files” http://java.sun.com/docs/books/tutorial/

deployment/jar/index.html

SE-2030Dr. Rob Hasker

16