creating jar files

Upload: nabeel-farooq

Post on 07-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Creating Jar Files

    1/19

    Creating Jar Files

  • 8/6/2019 Creating Jar Files

    2/19

    7/2/20112

    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 other files may also be included.

    Jar files can be run on Windows by double

    clicking jar files if the JVM is installed.

  • 8/6/2019 Creating Jar Files

    3/19

    7/2/20113

    Why use a JAR File?

    Compression:Jar files reduce the size of theoriginal files.

    Speed: The applet can be downloaded in one httptransaction.

    Security: Jar files can be signed digitally. Userswho recognize the signature can optionally grantpermission to run the file or refuse.

    Package sealing: Sealing a package within the jarfile means that all classes defined in that packageare found in the same jar file.

  • 8/6/2019 Creating Jar Files

    4/19

    7/2/20114

    Flow Chart

    Create amanifest.mf file

    Create a jar file

    Basic jar commands

    JAR subdirectories

    Execute the jar file

  • 8/6/2019 Creating Jar Files

    5/19

    7/2/20115

    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

  • 8/6/2019 Creating Jar Files

    6/19

    7/2/20116

    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

  • 8/6/2019 Creating Jar Files

    7/19

    7/2/20117

    How to create a jar file

    The execution of this command creates

    the file:

    Foo.jar

  • 8/6/2019 Creating Jar Files

    8/19

    7/2/20118

    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

  • 8/6/2019 Creating Jar Files

    9/19

    7/2/20119

    Executing Jar AppletsApplets 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.

  • 8/6/2019 Creating Jar Files

    10/19

    7/2/201110

    Executing Jar Applets For example, using the applet tag for your

    Foo.jar archive, you would use the

    following:

  • 8/6/2019 Creating Jar Files

    11/19

    7/2/201111

    Creating Jar Files:

    Advanced meth

    od The more formal and complete way to

    create a jar file is described in the

    following slides.

  • 8/6/2019 Creating Jar Files

    12/19

    7/2/201112

    Createmanifest.mf

    Create a new notepad file named manifest.mf.This file contains a signal line that points out themain class. Here is the signal line.

    Main-Class:name of main class

    Blank line must be included because someWindows OS versions need it.

    For example: myClass is my main Class. Somanifest.mf is following.

    Main-Class: myClass

  • 8/6/2019 Creating Jar Files

    13/19

    7/2/201113

    Jar file and Jar Commands

    Next, create a jar file using the jar command injava. Here is the command.

    jar cvmf .jarmanifest.mf A.class B.class..

    There are spaces between each file and classname.

    Here are the basic jar commandsCreate Jar file => jar cf jar-file input-file

    View Jar file => jar tf jar-file

    Extract Jar file => jar xf jar-file

  • 8/6/2019 Creating Jar Files

    14/19

    7/2/201114

    JAR Subdirectories

    The JAR format also support storing files

    in a directory structure. Consider the

    following structure.

  • 8/6/2019 Creating Jar Files

    15/19

    7/2/201115

    JAR Subdirectories

  • 8/6/2019 Creating Jar Files

    16/19

    7/2/201116

    JAR Subdirectories

    If we want keeping the same structure, we

    can compress files by typing:

    jar cmf Sample.mf Sample.jarSample.class Turtle.class Sample.java

    Turtle.java images

  • 8/6/2019 Creating Jar Files

    17/19

    7/2/201117

    JAR Subdirectories

    The contents listing appears as:

    META-INF/

    META-INF/MANIFEST.MFSample.class

    Turtle.class

    Sample.java

    Turtle.java

    images/ images/image1.gif

    images/image2.gif

    images/image3.gif

  • 8/6/2019 Creating Jar Files

    18/19

    7/2/201118

    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 jarjar-file

  • 8/6/2019 Creating Jar Files

    19/19

    7/2/201119

    Common Mistakes

    Forgetting to leave a blank line after the

    main class declaration in the manifest.mf.

    OS may return Fail to load Main-Classmanifest attribute error.

    Failing to compress all your files. You

    have to type in all the files you want tocompress. Separate these files with a

    space.