Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.6. JAR files

5.6. JAR files
Last updated: 27 January 2013.

The JAR file format (Java ARchive) is a file format that is built on the ZIP file format with additional features. A JAR file is a file with a .jar extension containing the class files and resources (images, language files,...) of an application.

Hence, a JAR file allows you to deliver your application in a single compressed file instead of delivering the class files and resources separately. Moreover, a JAR file can be executable, that is, the user can run it by simply double clicking it on certain systems or, alternatively, by typing a command in a terminal (Linux and Mac) or command prompt (Windows). Naturally, it is possible to extract the files contained in a JAR file.

The JDK provides a tool named jar that lets the user create jars, view their content, extract their content and also update it.


5.6.1. How to create a JAR file

The command to create a JAR file looks something like this:

jar  cvf  resultingJarFile.jar  inputFilesList

In the above command, 3 options are used (v is optional):The argument resultingJarFile.jar is the name of the generated JAR file and inputFilesList is a list of files and/or directories separated by spaces. The wildcard character *  can be used in the list. If there are directories in the list, their contents are added recursively to the JAR file. Typically, the list of files includes the directory where the class files of your application reside. Usually, a JAR file does not contain any source files.

By default, a compressed JAR file is generated. If you don't want to generate a compressed JAR file, use the option 0 (zero). Uncompressed JAR files are faster to generate but they take longer to deliver across the internet due to larger file sizes. The command to generate an uncompressed JAR file looks like this:

jar  cvf0  resultingJarFile.jar  inputFilesList

As an example, let's say the class files and resources of your application have the following directory structure:

/com/javaperspective/App.class
/com/javaperspective/Practice.class
/com/javaperspective/resources/language/package.properties
/com/javaperspective/resources/images/Logo.gif

The character / used above is the Linux and Mac file separator. On a Windows system, the file separator is the backslash character \. In order to create a JAR file named javaperspective.jar containing all the files of the application, you would change to the directory containing the com directory and type this command:

jar  cvf  javaperspective.jar  com

As a result, the generated JAR file javaperspective.jar would contain the following files:

META-INF/MANIFEST.MF
com/javaperspective/App.class
com/javaperspective/Practice.class
com/javaperspective/resources/images/Logo.gif
com/javaperspective/resources/language/package.properties

As you can see, a file named MANIFEST.MF whose purpose is to hold additional information about the JAR file is by default automatically created. If you don't want the manifest file to be created, use the option M. The manifest file will be discussed later in the section 5.6.5. (The Manifest file).


5.6.2. How to view the content of a JAR file

To view the content of a JAR file without unpacking it, type the following command:

jar  tf  jarFile.jar

Here is a summary of the options used in the command:You can use the option v  to set the verbose mode. In that case, additional information is displayed, such as the file sizes and creation dates. For example, the verbose mode is set in the command shown below:

jar  tvf  jarFile.jar

The argument jarFile.jar is the name of the JAR file.


5.6.3. How to extract the files contained in a JAR file

You can extract in the current directory all the files contained in a JAR file named jarFile.jar like this:

jar  xf  jarFile.jar

Only two options are used in the command:The argument jarFile.jar is the name of the JAR file. At the end of the extraction, the files are extracted in the current directory and they are in a directory structure that is identical to the one in the original JAR file. If there are files in the current directory that have the same pathname as extracted files, they are overwritten. Note that the original JAR file remains unchanged.

You can specify which files will be extracted as follows:

jar  xf  jarFile.jar  listOfFilesToBeExtracted

The argument listOfFilesToBeExtracted is a list of files separated by spaces. Only those files will be extracted from the JAR file.


5.6.4. How to update a JAR file

You can update a JAR file in 3 ways:
  1. By adding new files to it.
  2. By overwriting existing files.
  3. By modifying the content of the manifest file.
This section explains how to add new files to a JAR file and how to overwrite existing files in a JAR file. Modifying the content of a manifest file will be discussed in the next section (5.6.5. The Manifest file).

The option u allows you to add new files to an existing JAR file and/or overwrite files that are already bundled in the JAR file. Here is the command:

jar  uf  existingJarFile.jar  inputFilesList

The options are:The argument existingJarFile.jar is the JAR file whose content is to be updated and inputFilesList is a list of files separated by spaces. If the list contains files that are already bundled in the JAR file, those bundled files will be overwritten. The new files from the list are simply added to the JAR file.


5.6.5. The Manifest file

When a JAR file is created, it contains a default manifest file whose pathname is META-INF/MANIFEST.MF, unless the option M was specified in the command which created the JAR file. The content of such a default manifest file looks something like this:

Manifest-Version: 1.0
Created-By: 1.7.0_05 (Oracle Corporation)

The lines in the file are header:value pairs. You might have noticed that the default manifest file above only contains two header:value pairs that give information about the manifest's version and about the version of the JDK. In fact, the manifest file is the place where you can customize a JAR file by adding as many header:value pairs as necessary. To add custom header:value pairs to a manifest file, just follow these steps:
  1. Create a text file encoded in UTF8 and containing the custom header:value pairs. The text file must end with a new line.
  2. Create a JAR file with a command including the m option which adds the content of the specified text file to the automatically generated default manifest file.

The command that creates the JAR file follows:

jar  cfm  resultingJarFile.jar  customManifest.txt  inputFilesList

There are 3 options:The argument resultingJarFile.jar is the name of the generated JAR file. The argument customManifest.txt is a text file containing custom header:value pairs and inputFilesList is a list of files and/or directories separated by spaces. The options f and m must be in the same order as the arguments resultingJarFile.jar and customManifest.txt.

Several headers are provided to let you customize a JAR file. In particular, you can do the following:

How to make a JAR file executable

Use the Main-Class header like this:

Main-Class: fullyQualifiedClassName

In the line above, fullyQualifiedClassName is the fully qualified class name of your application's entry point (the class containing the main method). Assuming that the main method is in the class App within the package com.javaperspective, the header:value pair would be the following:

Main-Class: com.javaperspective.App

The resulting JAR file can be executed by typing the command shown below:

java  -jar  resultingJarFile.jar


How to add other JAR files to a JAR file's classpath

Use the Class-Path header as follows:

Class-Path: otherJarFile1.jar   otherJarFile2.jar   otherJarFile3.jar   otherDirectory/otherJarFile4.jar   otherDirectory/otherJarFile5.jar

The value is a list of JAR files separated by spaces. In the above example, otherJarFile1.jar, otherJarFile2.jar and otherJarFile3.jar are in the current directory whereas otherJarFile4.jar and otherJarFile5.jar are in another directory (otherDirectory).


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.6. JAR files
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   5. Java in Practice  >   5.7. Classpath

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7