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 JAR (Java Archive) utility remains a component of the Java Development Kit (JDK) 22. The JAR utility is a core tool of the Java Development Kit, used for creating, managing, and extracting JAR files. It plays a vital role in Java development for packaging compiled Java classes, resources, and metadata into a single archive file, which can then be distributed and deployed easily. The JAR utility continues to be essential in JDK 22, with support for modern features, such as module handling introduced in Java 9, and updated to align with new developments in the Java ecosystem.
Key Functions of the JAR Utility in JDK 22
  1. Packaging:
    • Combine multiple files (like .class files and resources) into a single JAR file.
  2. Compression:
    • Compress files to save space and speed up file transfer.
  3. Metadata:
    • Include manifest files to specify metadata like the main class or module information.
  4. Module Support:
    • Work with modular JAR files, introduced with the Java Platform Module System (JPMS).
  5. Management:
    • Update or view the contents of existing JAR files.
  6. Security:
    • Sign and verify JAR files for authentication and integrity.

Compatibility: The JAR tool is backward-compatible, meaning that features from previous Java versions are still available, and archives created with older JDKs can be used with newer versions of the JDK, including JDK 22.
  • Module 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


History of the jar file: 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.
The `jar` command in Java is a versatile tool used to manage Java Archive (JAR) files. It serves several functions related to creating, updating, extracting, and inspecting JAR files, which are essentially compressed packages of Java classes, resources, and metadata.
Key Functions of the `jar` Command
  1. Creating JAR Files
    • Packages Java class files and resources (e.g., images, configuration files) into a single archive.
    • Commonly used to distribute Java applications or libraries.
    • Command Syntax:
      jar cf <jar-file-name> <files-or-directories>
      
    • cf: Creates a new JAR file.
    • Example:
      jar cf MyApp.jar com/example/*.class
      
  2. Viewing the Contents of a JAR File
    • Lists the files and directories within an existing JAR file without extracting them.
    • Command Syntax:
      jar tf <jar-file-name>
              
    • tf: Lists the table of contents.
    • Example:
      jar tf MyApp.jar
              
  3. Extracting Files from a JAR
    • Unpacks the contents of a JAR file into the current directory or a specified location.
    • Command Syntax:
      jar xf <jar-file-name>
              
    • xf: Extracts files.
    • Example:
      jar xf MyApp.jar
              
  4. Updating an Existing JAR File
    • Adds or replaces files within an existing JAR archive.
    • Command Syntax:
      jar uf <jar-file-name> <files-or-directories>
              
    • uf: Updates the JAR file.
    • Example:
      jar uf MyApp.jar newResource.txt
              
  5. Creating an Executable JAR File
    • Enables a JAR to be run directly using the java -jar command by specifying a Main-Class in the MANIFEST.MF file.
    • Steps:
      1. Create a manifest file (manifest.txt) containing:
        Main-Class: com.example.MainClass
                    
      2. Use the jar command:
        jar cfm MyApp.jar manifest.txt com/example/*.class
                    
    • Example to run the JAR:
      java -jar MyApp.jar
              
  6. Compressing JAR Files
    • Uses the ZIP file format for compression, which reduces the file size of the JAR.
  7. Signing JAR Files
    • When used with tools like jarsigner, the jar command helps create JAR files that can be digitally signed for security purposes.
  8. Verifying JAR Contents
    • Ensures the integrity of a JAR file when it contains a digital signature.




Additional Options
  • Verbose Output:
    • Add v to the command for verbose output, showing detailed information.
    • Example:
                jar cvf MyApp.jar com/example/*.class
              
  • Specifying a Manifest File:
    • Use the m option to include a custom MANIFEST.MF.
    • Example:
                jar cmf manifest.txt MyApp.jar com/example/*.class
              

Use Cases of the `jar` Command
  • Application Deployment: Bundles Java applications for easy distribution.
  • Library Packaging: Packages reusable Java libraries for inclusion in other projects.
  • Executable Archives: Creates runnable JAR files with a specified entry point.
  • Resource Management: Packages non-code resources (e.g., images, properties files) alongside Java classes.

The `jar` command is an essential tool in the Java development ecosystem, facilitating the organization and deployment of Java applications and libraries.


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.
In the next lesson, you examine how Java archives are used to package and distribute JavaBeans components.

SEMrush Software