Java Beans   «Prev  Next»
Lesson 1

Java Archives Files and Beans

This module shows you how to use the Java Archive (JAR) utility that comes standard with the JDK to examine JAR files and package Beans in them. The Java Archive (JAR) utility is still a component of the latest JDK 21.** It remains an essential tool for packaging and managing Java applications and libraries.

Here's a breakdown of JAR's presence in JDK 21:

  • JAR tool: The command-line `jar` tool is still included in the `bin` directory of the JDK installation. You can use it to create, extract, list, and update JAR files.
  • JAR API: The Java classes for working with JAR files, such as `java.util.jar.JarFile` and `java.util.jar.JarEntry`, are still part of the Java API and can be used in your Java code.
  • JAR format: The JAR file format itself hasn't changed significantly, so JAR files created with older JDK versions are still compatible with JDK 21.
However, there are a few changes to note:
  • JAR index feature removed: The `-i` or `--generate-index` options for the `jar` tool, which were used to create index files for faster JAR loading, have been removed in JDK 21. This is because the Java Runtime Environment (JRE) now generates its own indexes automatically.
  • JAR signing enhancements: JDK 21 includes improvements to JAR signing, such as increased maximum signature file size and support for new signature algorithms.
Overall, the JAR utility remains a core part of JDK 21, and you can continue to use it to manage your Java applications and libraries.**


Module Learning Objectives

After completing the module, you will have the knowledge and skills necessary to:
  1. Examine existing JAR files with the JAR utility
  2. Package Beans in JAR files for distribution
In the next lesson, you examine how Java archives are used to package and distribute JavaBeans components.

Version 1.1 of the JDK

Version 1.1 of the JDK introduced the Java Archive (or JAR) file. JAR file archives can contain any number of files, and they can provide compression based upon the ZIP format. JAR files can be used for packaging related class files, serialized Beans, and other resources. This scheme allows multiple Beans to be packaged in a single JAR file, providing a convenient way to share common class files and resources. Optionally, a JAR file may contain a manifest describing its contents. We will be taking advantage of JAR files in later chapters to bundle Beans and their related support classes. Although this is the preferred way of packaging Beans, there is another reason to make use of JAR files. JAR files can also be used to improve the download performance of Java applets that are retrieved from the Web using HTTP.

SEMrush Software

Which function does the jar command fulfill in Java

The jar command is a command-line tool that is included with the Java Development Kit (JDK) and is used for creating, updating and managing Java Archive (JAR) files. JAR files are similar to ZIP files, but are specifically designed for Java, and include metadata that is used by the Java runtime. The jar command has several functions, including:
  1. Creating JAR files: The jar command can be used to create JAR files from a directory of class files or other resources, such as images and text files. The command can also be used to specify the main class of the JAR file, which is the class that will be executed when the JAR file is run.
  2. Extracting files from JARs: The jar command can also be used to extract files from an existing JAR file.
  3. Updating JAR files: The jar command can be used to update the contents of a JAR file without having to recreate the entire archive.
  4. Viewing the contents of JARs: The jar command can be used to view the contents of a JAR file, including the list of classes, images, and other resources that it contains.
  5. Signing JARs: The jar command can also be used to sign JAR files with digital certificates, which is a way to verify the authenticity and integrity of the JAR file.

The jar command is a powerful tool for managing JAR files in Java. It allows to create, extract, update, view and sign JAR files, providing a lot of functionality for managing the distribution, execution and security of Java applications.


jar Program

The jar program is provided as part of the JDK. It can be used to create JAR files, extract some or all of their contained elements, and list their contents. The jar program is executed from the command-line prompt. Its usage is:
jar {ctx}[vfm0M] [jar-file] [manifest-file] files ...

One, and only one, of the required command-line options (c, t, or x) can be selected at a time. The rest of the options can be combined or omitted as needed. The optional jar-file argument is used to specify the name of the archive file. This argument should be specified when the command-line option f is used. There is no required naming convention for JAR files. They can use any naming style, and use any file extension supported by the native operating system. However, the tools that use JAR files are free to mandate their own naming conventions.
The optional manifest-file argument is used to specify the name of a file that contains the manifest information to be used for the archive. The manifest-file should be specified when the commandline option m is used.