1 jar-files

weaverchurchSoftware and s/w Development

Aug 15, 2012 (5 years and 2 months ago)

224 views



1

jar
-
file
s

1.1

Summary

Jar files are a way of packaging a java application as a single executable file.
A jar file can contain
bytecode (.class files) as well as data files, graphics images and other resources belonging to a java
application.

A jar
-
file is ess
entially a zip file with special features. You can easily view the contents of a jar file by
renaming the file to .zip ( or adding the .zip extension) and the use software like WinZip of Windows
XP’s internal.
Zip file viewer.
Zip tools cannot create jar f
iles!

Jar files contains features not
implemented in standard zip tools.

The purpose of this document is to provide a brioef description of jar
-
files and to provide an example
of

how a executable application in the form of a jar file can be created.

1.2

Thje
structure of a jar file

The netbeans environment creates jar
-
f
iles whenever a project is
successfully

built
.
This example is
given for the .jar file provided by netbeans building the KeyboardReading example. You find the jar
file in your Project directory
in the subdirectory named dist, see below.



The jar
-
file is renamed with a .zip extension for easy viewing in WinXP.


Thi zip
-
file displays the package created in the keyboardreading example
, keyboardreading
.

1.3

The
jar manifest

Most of the features provid
ed by the jar format are contained within the folder META
-
INF.

The
META
-
INF folder contains metadata (information about the
jar
-
file and the
application)
.
Nothing
required by the application itself is located within this folder.





The information is cont
aing in a file called MANIFEST.MF
.
Manifest.mf
is a plain text file with the
.mf extension.
The format of the Manif
est.mf file is defined by jar.
Example


This instruction are created by the NetBeans environment.
The information in the manifest is line
-
ba
sed key
-
value pairs.

Manifestets form

NAME
:
VALUE

The key name/value pairs being

Manifest
-
Version: 1.0

Main
-
Class: keyboardreading.KeyboardReading

This provides the java environment with the instruction
s required to execute the application.
In particular

the class containing the public static void main(String[]arg) method.


1.4

Creating jar files

You create a jar file by using the tool jar.exe. jar.exe is a command
-
line tool located in the java bin
directory.

Locate you java installation directory and locat
e the bin/jar.exe file. Y
ou command tool
should have a path set to the java installation directory.




Ex
a
mpl
e

2
the help for creating the
.
jar file.
Execute the command in your environment to get the help
contents given in english.

jar cvfm
ApplicationNam
e

Mainfest
-
file

Class
-
files

Suppose we have written an application where the class containing the m
ain method is named
StartClass (of StartClass.class) and the required files, including StartClass.class, are located in
a directory java.

1


Create the mani
fest file using Notepad or similar suitable tool. The manifest must contain at a
minimum.

Manifest
-
Version: 1.0

Main
-
Class:
StartClass

Create this file in the same directory as the class files.

2


Type the following command while in the directory java/

j
ar

cvfm
MyApplication
.jar manifest.txt *.class


The order of
cvfm
is important. c
-
create, v
-
verbose,

f
-
file (
supply a filename for the application/jar
file),
m
-
manifest(
add a given manifest file
).

Something similar to the above will display.
.

The jar tool

creates a file
MyApplication
.jar
. You can execute this application by issuing the command
java

jar MyApplication.jar

1.4.1

Configuring windows to execute

jar
files

You java install procedure may already have provoded you with this particular feature. If this
is the
case you shold not execute these instructions.




In the explorer interface select menu

Tools


s
elect
Folder Options


select tab
Fil
e

Types



select
New... type
jar
and
Ok.


With the jar filetype selected select
A
d
v
anced


If the open action is no
nexistent select New
… If it exist select Edit…




In the edit field A
pplication used
… navigate to your java installation directory.
Select the java
interpreter
javaw.exe.
Add the parameters
-
jar "%1" %*

"C:
\
Program Files
\
Java
\
jre1.5.0_05
\
bin
\
javaw.exe"
-
j
ar "%1" %*

or

"C:
\
Program
Files
\
Java
\
jre1.5.0_05
\
bin
\
java
.exe"
-
jar "%1" %*

J
ava.exe
should be used if you would like to be able to execute console based applications.

This will associate .jar files with the java runtime environment.
Now you
r

.jar files w
ill be
executable.
You can execute you jar applica
t
ion
file by double
-
clicking your .jar file or by
writing

start
MyApplication
.jar

or just

MyApplication
.jar

At a command prompt. The first statement will execute the application in a new process
(own
wind
ow in the console case), the second executes the application in the same console.