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):
- c is to specify that you want to create a JAR file.
- v sets the verbose mode. While the JAR file is being generated, information about the files being added is displayed to the standard output.
- f is to specify that the result must be bundled in a JAR file instead of being displayed to the standard output.
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
/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
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:
- t is to specify that you want the JAR file's table of contents.
- f is to specify that the name of the JAR file is an argument of the command.
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:
- x is to specify that you want to extract the content of the JAR file.
- f is to specify that the name of the JAR file is an argument of the command.
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:
- By adding new files to it.
- By overwriting existing files.
- By modifying the content of 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:
- u is to specify that you want to update the JAR file.
- f is to specify that the name of the JAR file is an argument of the command.
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)
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:
- Create a text file encoded in UTF8 and containing the custom header:value pairs. The text file must end with a new line.
- 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:
- c is to specify that you want to create a JAR file.
- f is to specify that the name of the JAR file is an argument of the command.
- m is to specify that the header:value pairs contained in the text file customManifest.txt should be added to the default manifest file.
Several headers are provided to let you customize a JAR file. In particular, you can do the following:
- Make a JAR file executable.
- Add other JAR files to a JAR file's classpath.
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
Next tutorial : JavaPerspective.com > Beginner Tutorials > 5. Java in Practice > 5.7. Classpath