What is a Jar File? Java archive (jar) files are compressed files - - PowerPoint PPT Presentation

what is a jar file
SMART_READER_LITE
LIVE PREVIEW

What is a Jar File? Java archive (jar) files are compressed files - - PowerPoint PPT Presentation

Creating Jar Files Based on slides by: Jin Hung, Gregory Olds, George Blank, Sun Java Web Site What is a Jar File? Java archive (jar) files are compressed files that can store one or many files. Jar files normally contain java or class


slide-1
SLIDE 1

Creating Jar Files

Based on slides by: Jin Hung, Gregory Olds, George Blank, Sun Java Web Site

slide-2
SLIDE 2

What is a Jar File?

  • Java archive (jar) files are compressed files that

can store one or many files.

  • Jar files normally contain java or class files, but
  • ther files may also be included.
  • Jar files can be run on Windows by double

clicking jar files if the JVM is installed.

slide-3
SLIDE 3

Why use a JAR File?

  • Compression:Jar files reduce the size of the original files.
  • Speed: The applet can be downloaded in one http

transaction.

  • Security: Jar files can be signed digitally. Users who

recognize the signature can optionally grant permission to run the file or refuse.

  • Package sealing: Sealing a package within the jar file

means that all classes defined in that package are found in the same jar file.

slide-4
SLIDE 4

Flow Chart

  • Create a manifest.mf file
  • Create a jar file
  • Basic jar commands
  • JAR subdirectories
  • Execute the jar file
  • Common mistakes
  • Bibliography
slide-5
SLIDE 5

How to create a jar file: Simple Method

  • The jar command a utility that comes with the

JDK.

  • The format of the basic format of the jar

command is:

jar cf jar-file input-files

slide-6
SLIDE 6

How to create a jar file

  • Take as an example the Java application

made from the files

– foo.class – foobar.class

  • To make a jar file use the command

jar cvf foo.jar foo.class foobar.class

  • Note that each file is separated by a space
slide-7
SLIDE 7

How to create a jar file

  • The execution of this command creates the

file:

– foo.jar

slide-8
SLIDE 8

Executing Jar Application

  • Jar applications can be run with the following

basic command:

– java -jar jar-file

  • So to run our foo.jar if it was an application

use:

– java –jar foo.jar

slide-9
SLIDE 9

Executing Jar Application

  • Many Operating systems such as the Mac OS

will allow the execution of jar applications by simply double clicking on them.

slide-10
SLIDE 10

Executing Jar Applets

  • Applets are invoked from applet tags in HTML

code.

  • To execute an Applet simply add the name of

the jar file into the applet tag is under the archive parameter.

slide-11
SLIDE 11

Executing Jar Applets

  • For example, using the applet tag for your

Foo.jar archive, you would use the following:

<applet code=TicTacToe.class archive=“foo.jar" width=120 height=120> </applet>

slide-12
SLIDE 12

Creating Jar Files: Advanced method

  • The more formal and complete way to create

a jar file is described in the following slides.

  • Note that the more complex your process, the

more difficulty a beginning Java user is going to have with it.

slide-13
SLIDE 13

Create manifest.mf

Create a new notepad file named manifest.mf. This file contains two lines that define the author and the main class and a third line that is blank: Created By:<space>Name ( student number ) Main-Class:<Space>name of main class <blank line> For example: for the demo program manifest.mf is: Created By: Hugh Murrell (7525684562) Main-Class: BigIntegerDemo <blank line>

slide-14
SLIDE 14

Jar file and Jar Commands

  • Next, create a jar file using the “jar” command in
  • java. Here is the command.

jar cvfm <the jar file name>.jar manifest.mf

a.class b.class……..

There are spaces between each file and class name.

§ Here are the basic jar commands

Create Jar file => jar cf jar-file input-file View Jar file => jar tf jar-file Extract Jar file => jar xf jar-file

slide-15
SLIDE 15

JAR Subdirectories

  • The JAR format also support storing files in a

directory structure. Consider the following structure:

slide-16
SLIDE 16

JAR Subdirectories

slide-17
SLIDE 17

JAR Subdirectories

  • If we want to keep the same structure, we can

compress files by typing: jar cvfm Sample.jar Sample.mf Sample.class

Turtle.class Sample.java Turtle.java images

slide-18
SLIDE 18

JAR Subdirectories

  • The contents listing appears as:

META-INF/ META-INF/MANIFEST.MF Sample.class Turtle.class Sample.java Turtle.java images/ images/image1.gif images/image2.gif images/image3.gif

slide-19
SLIDE 19

Executing the Jar File

  • There are two ways to execute jar files.
  • 1. If an OS can read *.jar as javaw.exe or

java.exe, users can double click jar files on the window. Otherwise:

  • 2. In a command window, type in

java –jar jar-file

slide-20
SLIDE 20

Viewing manifest information

To view then contents of your manifest file I will issue the shell command:

  • unzip –p demo.jar META-INF/MANIFEST.MF

the –p option for unzip causes output to be sent to the console.

slide-21
SLIDE 21

Common Mistakes

  • Forgetting to leave a blank line after the main class

declaration in the manifest.mf. OS may return “Fail to load Main-Class manifest attribute” error in Windows machines.

  • Failing to compress all your files. You have to type in

all the files you want to compress. Separate these files with a space.

slide-22
SLIDE 22

Bibliography

  • Packaging Programs in JAR Files, and

Running JAR-Packaged Software http://java.sun.com/docs/books/tutorial/ deployment/jar/index.html