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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in Java )
Step by Step guide to setup freetts for Java
Open a .docx file and show content in a TextArea using Java
concurrent.Flow instead of Observable class in Java
DateFormat sample program in Java
Simple Port Scanner application using Java
Using the AWS SDK for Java in Eclipse
Read a file having a list of telnet commands and execute them one by one using Java
Calculator application in Java
Latest Articles (in Java)
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)
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)
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate