Unformatted text preview:

Creating Jar FilesWhat is a Jar File?Why use a JAR File?Flow ChartHow to create a jar file: Simple MethodHow to create a jar fileSlide 7Executing Jar ApplicationSlide 9Executing Jar AppletsSlide 11Creating Jar Files: Advanced methodCreate manifest.mfJar file and Jar CommandsJAR SubdirectoriesSlide 16Slide 17Slide 18Executing the Jar FileCommon MistakesBibliographyCreating Jar Files Jin Hung, Gregory Olds, George Blank, Sun Java Web SiteWhat 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.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.Flow Chart•Create a manifest.mf file•Create a jar file•Basic jar commands•JAR subdirectories•Execute the jar file•Common mistakes•BibliographyHow 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-filesHow 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 commandJar cvf Foo.jar Foo.class Foobar.class•Note that each file is separated by a spaceHow to create a jar file•The execution of this command creates the file:–Foo.jarExecuting 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.jarExecuting Jar Application•Many Operating systems such as the Mac OS will allow the execution of jar applications by simply double clicking on them.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.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>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.Create manifest.mf•Create a new notepad file named manifest.mf. This file contains a signal line that points out the main class. Here is the signal line. Main-Class:<Space>name of main class <blank line>Blank line must be included because some Windows OS versions need it. For example: myClass is my main Class. So manifest.mf is following. Main-Class: myClass <blank line is here>Jar file and Jar Commands•Next, create a jar file using the “jar” command in java. Here is the command. jar cvmf <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-fileJAR Subdirectories•The JAR format also support storing files in a directory structure. Consider the following structure.•Note: CS 602 students have had a lot of difficulty getting files to work in sub-directories. If you have trouble, put all your files in your default Web directory.JAR SubdirectoriesJAR Subdirectories•If we want to keep the same structure, we can compress files by typing: jar cmf Sample.mf Sample.jar Sample.class Turtle.class Sample.java Turtle.java imagesJAR 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.gifExecuting 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-fileCommon 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.Bibliography•Xiaoping Jia, Object-Oriented Software Development Using Java, Addison Wesley, 2nd Edition, 2003.•Packaging Programs in JAR Files, and Running JAR-Packaged Software


View Full Document

NJIT CS 602 - Creating Jar Files

Download Creating Jar Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Creating Jar Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Creating Jar Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?