Java jar command examples
- Details
- Written by Nam Ha Minh
- Last Updated on 04 August 2019   |   Print Email
In this Java tools tutorial, I will help you understand how to use the jar command provided in the Java Development Kit (JDK) with various useful examples.
Table of contents:
- Creating a normal jar file
- Including/Excluding manifest file in the jar file
- Creating an executable jar file
- Viewing content of a jar file
- Updating content of a jar file
- Extracting content of a jar file
- Disabling compression for jar file
- Generating index information for jar file
- Using command line argument files
- Passing JRE options
You know, jar is Java archive tool that packages (and compresses) a set of files into a single archive. The archive format is ZIP but the file name usually has .jar extension. This tool is used for creating, updating, extracting and viewing content of jar files.
The executable file of this tool can be located under the JDK_HOME\bin directory (jar.exe on Windows), so make sure you include this path in the PATH environment variable in order to run this tool anywhere from the command line prompt.
The general syntax of jar command is as follows:
jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Type jar command without any arguments to see its syntax and options, or see the online documentation here. In this tutorial, we summarize the most common usages of the jar command for your quick reference. Suppose that we have a project with the following directory structure:
The following examples demonstrate how to use the jar tool to create a library jar file, create an executable one and view, update, and extract content of the generated jar file. In most cases, the current directory is SwingEmailSender.
1. Creating a normal JAR file
A normal jar file is the non-executable one, such as a library jar file or an applet jar file. The following command put all files under the build\classesdirectory into a new jar file called SwingEmailSender.jar:
jar cfv SwingEmailSender.jar -C build\classes .
Note that there is a dot (.) at the end which denotes all files. The c option is to create, the f is to specify jar file name, the v is to generate verbose output, and the -C is to specify the directory containing the files to be added. Here’s the output (some entries are removed for brevity):
added manifest adding: net/(in = 0) (out= 0)(stored 0%) adding: net/codejava/(in = 0) (out= 0)(stored 0%) adding: net/codejava/swing/(in = 0) (out= 0)(stored 0%) adding: net/codejava/swing/FileTypeFilter.class(in = 1019) (out= 589)(deflated 42%) adding: net/codejava/swing/JFilePicker.class(in = 2369) (out= 1195)(deflated 49%) adding: net/codejava/swing/mail/(in = 0) (out= 0)(stored 0%) adding: net/codejava/swing/mail/ConfigUtility.class(in = 1689) (out= 907)(deflated 46%) adding: net/codejava/swing/mail/EmailUtility.class(in = 2451) (out= 1210)(deflated 50%) adding: net/codejava/swing/mail/SwingEmailSender.class(in = 5695) (out= 2881)(deflated 49%)
In case the current directory is build\classes, the -C option can be omitted (but the jar file needs to be moved upward two levels). For example:
jar cfv ..\..\SwingEmailSender.jar .
If we want to add only files in a specific package:
jar cfv SwingEmailSender.jar -C build\classes net\codejava\swing\mail
That takes only files in the package net.codejava.swing.mail (recursively).
The following command adds only one class (SwingEmailSender.class) to the jar file:
jar cfv SwingEmailSender.jar -C build\classes net\codejava\swing\mail\SwingEmailSender.class
If we want to add multiple classes, separate them by spaces (with full path). Suppose that the current directory is build\classes, the following command adds two classes (SwingEmailSender.class and JFilePicker.class) to the jar file:
jar cf ..\..\SwingEmailSender.jar net\codejava\swing\mail\SwingEmailSender.class net\codejava\swing\JFilePicker.class
2. Including/Excluding manifest file in the JAR file
By default, the jar tool automatically creates a manifest file when generating a new jar file. If we don’t want to have the manifest created, use the M option as in the following example:
jar cfvM SwingEmailSender.jar -C build\classes .
In case we want to manually add an external manifest file, use the m option as in the following example:
jar cfm SwingEmailSender.jar manifest.txt -C build\classes .
Here, content of the manifest.txt is copied to the generated manifest file inside the jar file.
3. Creating an executable JAR file
An executable jar file (stand-alone desktop application) must have information about the application entry point (main class) defined in its manifest file. The entry point can be specified either directly on the command line (using the e option), or in an external manifest file.
The following command creates an executable jar file with the main class is net.codejava.swing.mail.SwingEmailSender:
jar cfe SwingEmailSender.jar net.codejava.swing.mail.SwingEmailSender -C build\classes .
If using an external manifest file, remember to specify the main class by adding the following entry to the manifest file. For example:
Main-Class: net.codejava.swing.mail.SwingEmailSender
And the following command creates an executable jar file with the main class is specified in the external manifest file (manifest.txt):
jar cfm SwingEmailSender.jar manifest.txt -C build\classes .
4. Viewing content of a JAR file
The t option is used in conjunction with the f and v to list table of contents of a jar file. For example:
jar tf SwingEmailSender.jar
Output:
META-INF/ META-INF/MANIFEST.MF net/ net/codejava/ net/codejava/swing/ net/codejava/swing/FileTypeFilter.class net/codejava/swing/JFilePicker.class net/codejava/swing/mail/ net/codejava/swing/mail/ConfigUtility.class net/codejava/swing/mail/EmailUtility$1.class net/codejava/swing/mail/EmailUtility.class net/codejava/swing/mail/SettingsDialog.class net/codejava/swing/mail/SwingEmailSender.class
Adding the v option will include file size and date time for each entry. For example:
jar tfv SwingEmailSender.jar
Output:
0 Tue Dec 03 20:26:02 ICT 2013 META-INF/ 68 Tue Dec 03 20:26:02 ICT 2013 META-INF/MANIFEST.MF 0 Tue Dec 03 14:30:36 ICT 2013 net/ 0 Tue Dec 03 14:30:36 ICT 2013 net/codejava/ 0 Tue Dec 03 14:30:36 ICT 2013 net/codejava/swing/ 1019 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/FileTypeFilter.class 2369 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/JFilePicker.class 0 Tue Dec 03 14:30:36 ICT 2013 net/codejava/swing/mail/ 1689 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/mail/ConfigUtility.class 2451 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/mail/EmailUtility.class 3700 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/mail/SettingsDialog.class 5695 Thu May 23 06:43:08 ICT 2013 net/codejava/swing/mail/SwingEmailSender.class
It’s also possible to list only files under a specific package. The following command lists only files under the package net.codejava.swing.mail:
jar tf SwingEmailSender.jar net/codejava/swing/mail
Output:
net/codejava/swing/mail/ net/codejava/swing/mail/ConfigUtility.class net/codejava/swing/mail/EmailUtility.class net/codejava/swing/mail/SettingsDialog.class net/codejava/swing/mail/SwingEmailSender.class
5. Updating content of a JAR file
The u option is used to update an existing jar file, e.g. adding new classes and removing/updating manifest file.
The following example adds/updates the classes under the package net.codejava.swing.download to the existing archive:
jar uf SwingEmailSender.jar -C build\classes net/codejava/swing/download
The following command updates everything but removes the manifest file:
jar ufM SwingEmailSender.jar -C build\classes .
And the following command updates everything including the manifest file:
jar ufm SwingEmailSender.jar manifest.txt -C build\classes .
6. Extracting content of a JAR file
To extract a jar file, use the x option in conjunction with the f and v. The following command extracts all content of the SwingEmailSender.jar file to the current directory:
jar xf SwingEmailSender.jar
Add the v option if we want verbose output:
jar xfv SwingEmailSender.jar
Output:
created: META-INF/ inflated: META-INF/MANIFEST.MF created: net/ created: net/codejava/ created: net/codejava/swing/ inflated: net/codejava/swing/FileTypeFilter.class inflated: net/codejava/swing/JFilePicker.class created: net/codejava/swing/mail/ inflated: net/codejava/swing/mail/ConfigUtility.class inflated: net/codejava/swing/mail/EmailUtility.class inflated: net/codejava/swing/mail/SettingsDialog.class inflated: net/codejava/swing/mail/SwingEmailSender.class
If we want to extract only files under a specific package, pass the package path after the jar file name. For example:
jar xfv SwingEmailSender.jar net/codejava/swing/mail
That extracts only files in the package net.codejava.swing.mail. Here’s the verbose output:
created: net/codejava/swing/mail/ inflated: net/codejava/swing/mail/ConfigUtility.class inflated: net/codejava/swing/mail/EmailUtility.class inflated: net/codejava/swing/mail/SettingsDialog.class inflated: net/codejava/swing/mail/SwingEmailSender.class
7. Disabling compression for JAR file
By default, the generated jar file is compressed as ZIP/ZLIB compression format. However, it’s possible to disable the default compression by specifying the 0 option. For example:
jar cfv0 SwingEmailSender.jar -C build\classes .
8. Generating index information for JAR file
The i option allows us to generate an INDEX.LIST file that contains information about the specified jar file, such as the dependent jar files and location of all packages. For example:
jar i SwingEmailSender.jar
The INDEX.LIST file is generated under the META-INF directory. Here’s a sample content of this file:
JarIndex-Version: 1.0 SwingEmailSender.jar net net/codejava net/codejava/swing net/codejava/swing/download net/codejava/swing/mail
9. Using command line argument files
This is a useful feature that shortens and simplifies a lengthy jar command line. Just create a text file that contains jar options and arguments (separated by spaces or new lines), and then execute the jar command in the following form:
jar @file1 @file2...
For example, create a classes.txt file containing the classes to be included as follows:
net/codejava/swing/mail/ConfigUtility.class net/codejava/swing/mail/EmailUtility.class net/codejava/swing/mail/SwingEmailSender.class
Then execute the following command:
jar cfv SwingEmailSender.jar -C build\classes @classes.txt
Create an options.txt file that contains all the options as follows:
cfve SwingEmailSender.jar net.codejava.swing.mail.SwingEmailSender -C build\classes
Then execute the following command:
jar @options.txt @classes.txt
10. Passing JRE options
The -J option allows us to pass options to the underlying Java Runtime Environment (JRE) when needed. For example:
jar cfv SwingEmailSender.jar -C build\classes . -J-Xmx128M
That passes the option -Xmx128M (maximum memory size is 128MB) to the runtime environment. Note that there must be no space between -J and the options.
References:
Other Java Tools Tutorials:
- java command examples
- javac command examples
- Java serialver command examples
- Understanding the triad tools javac, java and jar in JDK
- The Java Shell (jshell) Tutorial
- How to compile, package and run a Java program using command-line tools (javac, jar and java)
Comments
Who needs EMail Sender, there are many applications in which other yard files are useful.
Well, just create an example (EMail Sender) and off you go!
A quick example, and who cares about the readers?
Methodically and dicatically very questionable, the car can do otherwise,
why doesn't it do it?
No, the jar command can't create password-protected zip file. You need to use a third party library.
Thanks for your article. I am looking to create a ZIP file with Password protected. Is it possible to do with Jar command? What will be the command to do it?
Regards,
Harsha