Using jar utility in Java - A jar tutorial
By: Emiley J in Java Tutorials on 2007-10-14
The jar tool combines multiple files into a single JAR archive file. jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format. However, jar was designed mainly to facilitate the packaging of java applets or applications into a single archive. When the components of an applet or application (.class files, images and sounds) are combined into a single archive, they may be downloaded by a java agent (like a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. jar also compresses files and so further improves download time. In addition, it allows individual entries in a file to be signed by the applet author so that their origin can be authenticated. The syntax for the jar tool is almost identical to the syntax for the tar command. A jar archive can be used as a class path entry, whether or not it is compressed.
Typical usage to combine files into a jar file is:
C:\Java> jar cf myFile.jar *.class
In this example, all the class files in the current directory are placed into the file named "myFile.jar". A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. The manifest file is the place where any meta-information about the archive is stored as name : value pairs.
To add all the files in a particular directory to an archive (overwriting contents if the archive already exists). Enumerating verbosely (with the v option) will tell you more information about the files in the archive, such as their size and last modified date.
C:\Java> dir 12/09/96 12:20a <DIR> . 12/09/96 12:17a <DIR> .. 12/09/96 12:18a 946 1.au 12/09/96 12:18a 1,039 2.au 12/09/96 12:18a 993 3.au 12/09/96 12:19a 48,072 spacemusic.au 12/09/96 12:19a 527 at_work.gif 12/09/96 12:19a 12,818 monkey.jpg 12/09/96 12:19a 16,242 Animator.class 12/09/96 12:20a 3,368 Wave.class 10 File(s) 91,118 bytes C:\Java> jar cvf bundle.jar * adding manifest adding: 1.au adding: 2.au adding: 3.au adding: Animator.class adding: Wave.class adding: at_work.gif adding: monkey.jpg adding: spacemusic.au
If you already have separate subdirectories for images, audio files and classes, you can combine them into a single jar file:
C:\Java> dir 12/09/96 12:11a <DIR> . 12/09/96 12:17a <DIR> .. 12/03/96 06:54p <DIR> audio 12/06/96 02:02p <DIR> images 12/09/96 12:10a <DIR> classes 5 File(s) 207,360 bytes C:\Java> jar cvf bundle.jar audio classes images adding: audio/1.au adding: audio/2.au adding: audio/3.au adding: audio/spacemusic.au adding: classes/Animator.class adding: classes/Wave.class adding: images/monkey.jpg adding: images/at_work.gif C:\Java> dir 12/09/96 12:11a <DIR> . 12/09/96 12:17a <DIR> .. 12/09/96 12:11a 207,360 bundle.jar 12/03/96 06:54p <DIR> audio 12/06/96 02:02p <DIR> images 12/09/96 12:10a <DIR> classes 6 File(s) 207,360 bytes
To see the entry names in the jarfile, use the "t" option:
C:\Java> jar tf bundle.jar META-INF/ META-INF/MANIFEST.MF audio/1.au audio/2.au audio/3.au audio/spacemusic.au classes/Animator.class classes/Wave.class images/monkey.jpg images/at_work.gif
To add an index file to the jar file for speeding up class loading, use the -i option.
Let's say you split the inter-dependent classes for a stock trade application, into three jar files: main.jar, buy.jar, and sell.jar. If you specify the Class-path attribute in the main.jar manifest as:
Class-Path: buy.jar sell.jar
then you can use the -i option to speed up your application's class loading time:
C:\Java> jar i main.jar
An INDEX.LIST file is inserted to the META-INF directory which will enable the application class loader to download the specified jar files when it is searching for classes or resources.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments