Java Beans   «Prev  Next»
Lesson 4Working with JAR files
ObjectiveUse the JAR utility to manipulate JAR files.

Working with JAR Files

This lesson discusses how to use the JAR utility to manipulate JAR files. Like all other JDK tools, the JAR utility is a command-line tool. Its syntax follows:
jar

Jar Argument Options
The Files argument specifies the files to be used when working with a JAR file and varies according to the Options.

JavaBeans options argument

Unlike most command-line tools, the JAR utility does not require the use of a / or - when specifying options.
In Java SE 17, the `jar` utility is used for creating, viewing, extracting, and managing JAR files. It offers several parameters (also called options) that can be classified as mandatory or optional depending on the operation being performed. Here's a breakdown:
Mandatory Parameters
  1. Main Operation Option:

    You must specify one of the following main operation options to define the action to be performed:

    • -c (create a new archive)
    • -t (list the contents of an archive)
    • -x (extract files from an archive)
    • -u (update an existing archive)
    • -d (describe the module)
    • -i (generate the index information for JAR service provider configuration files)

    Example: jar -c ... indicates you're creating a JAR file.

  2. File Argument (for most operations):
    • The -f option typically requires a filename to specify the JAR file being operated on.

    Example: jar -cf myarchive.jar ...


Exceptions: Operations like `-t` or `-x` may not need `-f` if you want to operate on the default `stdin`.


Optional Parameters: These options can be used in conjunction with the main operation options:
  1. File-Related Options:
    • -f (specify the archive file name)
    • -e (specify the entry point for standalone applications bundled in the JAR file)
  2. Verbose Output:
    • -v (generate verbose output about the operation)
  3. Manifest Handling:
    • -m (include a custom manifest file when creating/updating an archive)
    • -M (do not automatically create a manifest file)
  4. Compression Options:
    • -0 (store files without using ZIP compression)
    • -P (preserve file permissions in the archive)
  5. Indexing:
    • -i (generate an index file for JAR services)
  6. File Selection and Patterns:
    • Specify files or patterns at the end of the command to include specific files in the operation.



Examples
  1. Create a JAR file:
    jar -cf myarchive.jar -C build/classes .
        
    • -c: Create an archive
    • -f: Specify the JAR file name
    • -C: Change to the specified directory and include all files
  2. List contents of a JAR file:
    jar -tf myarchive.jar
        
    • -t: List contents
    • -f: Specify the JAR file to list
  3. Extract files from a JAR:
    jar -xf myarchive.jar
        
    • -x: Extract
    • -f: Specify the JAR file
  4. Add an entry point to a manifest:
    jar -cfm myarchive.jar manifest.txt -C build/classes .
        
    • -c: Create
    • -f: Specify the JAR file
    • -m: Include a custom manifest

Default Behavior If you do not specify the `-f` option, the `jar` command reads from or writes to the default standard input/output streams.


Options utilized by the JAR utility:

Below is a listing of the options utilized by the JAR utility:
Option Description
f Identify the name of the archive as the first file in the Files argument.
c Create a new archive.
m Create a manifest file for an archive based on an external manifest file. (The external manifest file is provided as the second file in the Files argument.)
t List the contents of an archive.
x Extract all the files in an archive, or just the files provided in the Files parameter.
v Output detailed information about the actions being performed.


Inside the JAR file

To find out what files are contained in a JAR file, you simply use the f and t options with the JAR utility, like this:
jar ft SomeArchive.jar

The f option is necessary to inform the JAR utility which JAR file's contents you would like to list.
The results of this command will include all the files contained within the SomeArchive.jar JAR file.

BeansBook.jar example:

We create an archive named BeansBook.jar that contains the GenericButtonAdapter class from the BeansBook.util package. We go to the directory that contains the BeansBook package directory, and execute the following command:
jar cvf BeansBook.jar BeansBook/util/GenericButtonAdapter.class

This command says that we are creating a new archive named BeansBook.jar, which contains the BeansBook/util/GenericButtonAdapter.class file, and that the jar program should produce some verbose output when it creates the archive. The resulting output looks like this:
adding: BeansBook/util/GenericButtonAdapter.class in=2999 out=1507
deflated 49.0%

This tells us that the BeansBook/util/GenericButtonAdapter.class file was added to the archive and that its size was deflated by 49% due to compression. Now let us examine the contents of the archive by executing the following command:
jar tvf BeansBook.jar

This command says that we want to list the table of contents of the archive file named BeansBook.jar. The program then produces the following output:
179 Sat Feb 15 16:27:04 EST 1997 META-INF/MANIFEST.MF
2999 Sat Feb 08 14:33:18 EST 1997
BeansBook/util/GenericButtonAdapter.class

The resulting verbose output shows that the archive contains a file named manifest.mf in directory META-INF. This is the manifest file for the archive. The JAR file also contains the file BeansBook/util/GenericButtonAdapter.class. This listing also includes the original size of each element along with its time stamp.

Extract manifest from BeansBook.jar archive

To get a better idea of what these entries look like, let us extract the manifest from the BeansBook.jar archive we just created, using the following command:
jar xf BeansBook.jar META-INF

This will extract everything in the archive from the directory META-INF, which includes the manifest file manifest.mf. If the META-INF directory does not already exist, it will be created when the manifest is extracted. The contents of the manifest look like this:
Manifest-Version: 1.0
Name: BeansBook/util/GenericButtonAdapter.class
Digest-Algorithms: MD5 SHA
MD5-Digest: wuX4KYNI+D3QYBTtNn6wdA==
SHA-Digest: R8cIwi1GSAgAdwAdrxb9AXlSBV8=

Jar Utility - Exercise

Click the Exercise link below to use the JAR utility to examine the buttons.jar JAR file that ships with the BDK.
Jar Utility - Exercise
In the next lesson, the JAR utility will be used to package a Bean.

SEMrush Software