Deploying Java applications as JAR files SE-2030 Dr. Mark L. Hornick 1 How to package an application...
-
Upload
felix-shields -
Category
Documents
-
view
214 -
download
1
Transcript of Deploying Java applications as JAR files SE-2030 Dr. Mark L. Hornick 1 How to package an application...
Deploying Java applications as JAR files
SE-2030Dr. Mark L. Hornick
1
How to package an application so that you can run it outside of Eclipse.
Consider an Eclipse project
SE-2030Dr. Mark L. Hornick
2
This application consists of files in two directories, in packages called demo.app and demo.data
When you run an application from within Eclipse,
it (internally) issues the following command:java –cp “D:\My Docs\se2030\JARDemo\bin”
demo.app/JARDemoApp
java is the command that runs the Java Virtual Machine Found at C:\Program Files\Java\jdk1.6.0_03\bin\java.exe
JARDemoApp is the name of the main class
demo.app is the package containing the main class
(demo.data is the name of the package containing the MyData class)
-cp <classpath> specifies the base directory where the .class file(s) for the packages are located
Not including the package subdirectories
SE-2030Dr. Mark L. Hornick
3
If your class files are in multiple file folders, you
must include them all:java –cp “D:\My Docs\se2030\JARDemo\bin; D:\MyDocs\
Documents\MSOE\Courses\Example Programs\se2030\UIHelper\bin” demo.app/JARDemoApp
-cp <classpath> specifies ALL base directories where the .class file(s) for the packages are located
Note that each directory is separated by a semicolon
SE-2030Dr. Mark L. Hornick
4
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. Mark L. Hornick
5
The jar utility can be used to create JAR files
jar cfm <jarfile> <manifest> -C <bin> <classfiles>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 informationNote : The contents of the manifest must be encoded in ansi.
-C <bin> specifies the location of the classfiles
classfiles specifies the files you want to place in the JAR file Separated by spaces Must include the full package folder path
SE-2030Dr. Mark L. Hornick
6
To bundle files in the same directory into a JAR file:
jar cfm JARDemoApp.jar manifest.txt –C ./bin demo/app/JARDemoApp.class –C ./bin demo/data/Data.class
This assumes: you are issuing the jar command from within the directory of
D:\My Docs\se2030\JARDemo manifest.txt is in the same directory Demo/app and demo/data are the names of the packages such that all class files
are located in D:\My Docs\se2030\JARDemo\bin\demo\app
or D:\My Docs\se2030\JARDemo\bin\demo\data 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)
SE-2030Dr. Mark L. Hornick
7
Manifest details
Manifest.txt is an text file containing the following text:
Manifest-Version: 1.0
Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.)
Main-Class: demo.app.JARDemoApp
SE-2030Dr. Mark L. Hornick
8
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\JARDemoApp.jar”
Or create a shortcut containing the above command
SE-2030Dr. Mark L. Hornick
9
If your JAR file references other JAR files (like Helper.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: demo.app.JARDemoApp
Class-Path: ./Helper.jar
Note that ./ specifies that Helper.jar in this case can be found in the same directory folder as JARDemoApp.jar (i.e. they are both in C:\temp).
SE-2030Dr. Mark L. Hornick
10
Online tutorial
“Packaging Programs in JAR files” http://java.sun.com/docs/books/tutorial/
deployment/jar/index.html
SE-2030Dr. Mark L. Hornick
11