CS 209 Programming in Java #12 JAR Files: Creation and Use Textbook Chapter 14 Spring, 2006 Instructor: J.G. Neal 1 Topics Introduction to JAR Files Using the JAR Tool for an Applet Deploying an Applet on a Web Server Example Program Using the JAR Tool for an Application Example Program 2
What is a JAR File? JAR file Acronym stands for Java ARchive file Provides a means of combining multiple files into a single JAR file Format is based on the ZIP file format Provides compression to achieve smaller file size Why is this beneficial? Used for both Applets and Applications 3 Purpose of JAR Files Purpose: To enable Java programs (applets or applications) and their components (e.g., Java.class files, images, sound files, etc.) to be combined into a single JAR file for deployment Applications can be easily delivered and installed using a single file Applets can be downloaded to a browser in a single HTTP transaction Avoids opening a new connection for each component file Improves performance and speed To provide compression of the Java program (including all of its files) Achieves smaller size Further improves download speed for applets Facilitates deployment of applications 4
JAR Files for Applets Archive attribute in Applet tag The Applet tag s archive attribute needs to be used to accommodate a JAR file Use the archive attribute to specify the location of the JAR file relative to the location of the HTML page, such as: archive = GridEx1.jar The archive attribute can actually list multiple JAR files, comma separated, such as: archive = classes.jar, images.jar, sounds.jar The code attribute is still required to specify the name of the applet where execution begins See the next slide for an example program that demonstrates the use of the Applet tag with archive attribute 5 Example Program Purpose: Demonstrate the use of a JAR file for an applet Provide an example HTML file containing an Applet tag with the required archive attribute Program: Folder: JarExs\CS209-Basic\ArithApplet Package name: examples Class name: Arith Questions: When you bring up an HTML page and get an error message such as Applet not inited, what are some of the possible causes of the problem? How would you fix the problem? Demonstrate the above 6
Example HTML File using JAR File <HTML> <TITLE>HTML Test Applet</TITLE> <BODY> <b>course: CS 209 Java Programming</b><br /> <b>program: Example Java Applet</b><br /> <hr /> <APPLET CODEBASE = "." ARCHIVE = "ArithApplet.jar" CODE = "examples.arith.class" NAME = "TestApplet" WIDTH = 400 HEIGHT = 300 HSPACE = 0 VSPACE = 0 ALIGN = Middle > </APPLET> </BODY> </HTML> 7 The JAR Tool Purpose: To combine multiple files into a single compressed JAR file Three types of input files to the JAR tool: Name of manifest file (optional) Name of destination JAR file Names of files or directories to be archived Command synopsis: jar [options] [manifest] destination FileOrDir [FileOrDir] Examples command lines: jar cfv GridEx1.jar layoutexs jar cf myjarfile *.class jar cmf mymanifestfile myjarfile *.class 8
The JAR Tool Options: c - Create a new archive t - Lists the table of contents x - Extracts all or named files f - Specifies the JAR file v - Verbose m - Manifest file name to include o - Store only, without compression M - Do not create manifest file u - Update JAR file i - Generate index information -c - Temporarily change directory -Joption - Pass option to the Java virtual machine 9 JAR Documentation JAR documentation comes as part of the JDK documentation For JAR File Details, see the JDK documentation: jdk1.5.x_xx\docs\tooldocs\windows\jar.html For JAR File Specification jdk1.5.x_xx\docs\guide\jar\jar.html 10
JAR Tool - Creating a JAR File Example command line invoking the jar utility : jar cfv GridEx1.jar layoutexs Explanation: jar is the name of the tool cfv are options: c specifies that a new archive file should be created f specifies that the second argument is the name of the jar file to create or process v specifies verbose output on stderr GridEx1.jar is the name of the JAR file to create layoutexs is the name of the folder of files and sub-folders to add to the JAR file 11 JAR Tool - Confirming JAR File Contents Example command line invoking the jar utility: jar tvf GridEx1.jar Explanation: Options tvf : t - indicates that the table of contents should be listed as output v - indicates that the output should be verbose; that is, show the file size in bytes, the date and time each file was created, and the path to the file including the file name f - indicates that the next item in the command line is the name of the Jar file Following the options is the name of the JAR file In this example: GridEx1.jar 12
Deploy Applet on Web Server Using JAR File Deploy applet on web server: Must have HTML page containing Applet or Object tag Create a folder in the document hierarchy of web server Copy the HTML page into the folder on web server Copy the JAR file into the folder on web server Example: Assume that the folder named GridColorEx has been created on server Copy the HTML page named GridEx1.html into the folder Copy the JAR file named GridEx1.jar into the folder 13 Example Program Example program: JAR File name: GridEx1.jar HTML File name: GridEx1.html Path on server: C:\Apache\htdocs\JavaExamples\GridColorEx URL: http://javaexamples/gridcolorex/gridex1.html Questions: What is the benefit of using a JAR file? If we want to make a change to the program Can we edit the program files in the JAR file? What steps would we take to make the change? 14
To Run the Example Program Assume that the main HTML page named GridEx1.html is in the following folder on the web server: JavaExamples/GridColorEx/ Bring up your web browser Enter the following URL into the Address slot: http://<hostname>/javaexamples/gridcolorex/gridex1.html If port number is required, enter: http://<hostname>:<port#>/javaexamples/gridcolorex/gridex1.html This should display the HTML page with embedded applet You should now be able to interact with applet 15 Example Applet Program 16
JAR Tool - Creating a JAR File for an Application Example usage: jar cfv GridExApplic.jar layoutexs Explanation: jar is the name of the tool cfv are options: c specifies a new archive file to be created f specifies that the second argument is a jar file to create or process v specifies verbose output on stderr GridExApplic.jar is the name of the JAR file to create layoutexs is the name of the folder of files and sub-folders to add to the JAR file 17 Run the Application from JAR File Command line to execute an application from a JAR file: java cp GridExApplic.jar layoutexs.gridexj The classpath parameter can actually include many JAR files Each JAR file can include many files The file (class) specified as the program to execute must include a method called main In the above example, the file is layoutexs.gridexj 18
Example Application Program 19