JAR Java ARchive Files


JAR Java ARchive Files Introduction

JAR is an acronym for Java ARchive. It is the zipping technique of Java. Always, the zipping styles are platform-dependent. A WinZip file can be zipped and unzipped on Windows platform only. The Java files, which are platform-independent, zipped with WinZip makes them platform-dependent (opens on Windows platform only). To overcome this, Java adopts a platform-independent way of zipping known as JAR. A number of files can be zipped with JAR and can be unzipped on any platform where Java is loaded. Wherever JVM exists, a JAR file can be created and unzipped on any JVM. Finally, to say, JAR is the zipping style of Java. The files to JAR (zip) can be of .txt, .doc, .gif or .rtf etc.

The advantages of JAR files

  1. Security: A JAR file can be used with digital certificates.
  2. Decreased download time: A JAR file is a zipped file and thereby occupies less memory. A JAR file can be downloaded from a Web server within less time.
  3. Compression: A JAR file occupies less space on the hard disk.

The extension of a JAR file should be .jar, of course, the command to zip is also jar. Sometimes, a JAR file may include a manifest file. A manifest file includes the meta-data containing the meta-information of files in the JAR file. Manifest file is optional.

jar is a command to zip the files and comes with JDK software. Many operations can be performed on JAR files. The options can be known by typing jar at the DOS prompt and pressing Enter key. Following code snippets give a few and frequently used JAR options.

1. Creating a JAR file

Think that you have the files like raju.doc, rao.txt and taj.gif. Now you would like to zip them with jar utility. Following is the way.

C:\way2java> jar cvf Misc.jar raju.doc rao.txt taj.gif

The options in the above statement are

c : create a JAR file.
v : display the verbose output at standard output.
f : the jar file name is included.

jar is the command to create a jar file. The particulars of compression before zipping and after zipping, known as verbose (v) should be displayed. f means, the jar file name is included. Here, the jar file name is Misc.jar.

Note: No spaces must be given in between cvf .

The above statement compresses the files. If compression is not required, the above statement can be modified as follows.

C:\way2java> jar cvf0 Misc.jar raju.doc rao.txt taj.gif

0 in the above statement indicates no compression is required. That is, files size before zipping and after zipping remains the same.

By default, the jar command creates a manifest file. The jar service keeps the particulars of zipping in this file.

2. Viewing the contents of a JAR file

After zipping, you may require to view the contents zipped. Following is the command.

C:\way2java> jar tf Misc.jar

where

t : table of contents.
f : jar file name is included.

The above statement displays all the files existing in the JAR file, Misc.jar.

Note: The options t and f can be in any order (but no spaces in between).

3. Extracting the contents of a JAR file

Let us extract (unzip) the jar file.

C:\way2java> jar xf Misc.jar

where x stands for extract the contents and f stands for the jar file name. The files are extracted into the same directory where jar file exists.

The above statement extracts all the files. But you can extract even one file also as follows.

C:\way2java> jar xf Misc.jar rao.txt

The above statement extracts rao.txt only from Misc.jar file.

4. Updating a JAR file

You are permitted to add one more file to the existing files in the jar. Following is the updating command.

C:\way2java> jar uf Misc.jar charminar.jpg

where

u : update the jar file.

Now the file charminar.jpg is added to the jar file.

JAR Options

Jar options gives you the operations on JAR file.

-c : create new archive (jar) file
-t : list of table of contents (to know the contents of jar file)
-x : extract the files from jar file
-u : update existing archive (you can add a file to the existing)
-v : generate verbose output at DOS prompt; means display the file sizes before zipping and after zipping
-f : specify archive file name (write the jar file name)
-m : include manifest information
-O : store only without zipping
-M : do not create manifest file
-i : generate index information
-c : change the specified directory and include the file mentioned

To know above options, just type jar and press enter key at the DOS prompt (when Java path is set).

4 thoughts on “JAR Java ARchive Files”

Leave a Comment

Your email address will not be published.