Javatpoint Logo
Javatpoint Logo

Working with JAR and Manifest files In Java

Java Archive (JAR) files are a common way to package and distribute Java applications. A JAR file is a compressed file format that contains Java class files, resources (such as images and properties files), and metadata. It simplifies the distribution of Java applications by bundling everything into a single archive.

Creating a JAR file - more Options

  • jar - file: It is the name of the JAR file on which we want to perform operations using the jar
  • file1, file2, file3: We want to add these files inside the JAR file. If any of these files are directories, the jar program processes them recursively, including all their contents.
  • manifest-file: It is the optional name of a file that contains the manifest of the JAR file. The manifest is a description of the archive's contents and origin. While every archive has a default manifest, users can provide their own to authenticate the contents of the archive.
  • c: Creates a new or empty archive and adds the specified files.
  • C: Temporarily changes the directory before processing the specified files.
  • e: Creates an entry point in the manifest, defining the main class for executable JAR files.
  • f: Specifies the JAR file name as the second command-line argument. If this parameter is missing, the jar tool will write the result to standard output when creating a JAR file or read it from standard input when extracting or listing a JAR file.
  • i: Creates an index file.
  • m: Adds a manifest file to the JAR. Users can supply their manifest to authenticate the contents of the archive.
  • M: Does not create a manifest file for the entries.
  • t: Displays the table of contents for the JAR file.
  • u: Updates an existing JAR file with new or modified files.
  • v: Generates verbose output, providing detailed information during the JAR creation process.
  • x: Extracts files from the JAR. Only those files are extracted if specific file names are provided; otherwise, all files are extracted.
  • 0: Stores files without ZIP compression.

Example

For creating a JAR file that has two classes, HelloWorld.class and Greet.class one needs to write the following command :


Working with JAR and Manifest files In Java

1. Create two Java Classes:

Filename: HelloWorld.java

Filename: Greet.java

2. Compile the Java Classes:

Save these files and proceed to compile them using the provided command:

3. Create a Manifest File:

Generate a text file named Manifest.txt and include the following content:

4. Create the JAR File:

Now, create the JAR file using the following command:

The above command creates a JAR file named HelloWorldApp.jar with the specified manifest file and both compiled class files.

5. Run the JAR File:

Finally, we can run the JAR file using the following command:

Output:

Working with JAR and Manifest files In Java

Manifest File

A manifest file in the context of JAR (Java Archive) files is a particular file that provides metadata about the contents of the JAR file. The manifest file is typically named MANIFEST.MF and is located in the META-INF directory within the JAR file. It includes information such as the main class for executable JAR files, version information, and other attributes.

Working with JAR and Manifest files In Java

Manifest files, used in JAR (Java Archive) files, organize information into sections, each with a name and a corresponding value. These sections help control various properties of the archive. Exercising caution is crucial when utilizing the `m` option in the `jar` command to update a manifest file. Incorrect modifications can lead to errors or unexpected issues. Therefore, when altering manifest files, it is essential to adhere to guidelines to guarantee the proper functionality of the JAR file. We may get the following error.

Things to keep in mind while handling Manifest files:

1. Including a space between the section name and its value in a manifest file is crucial. For instance, writing Version:1.1 is invalid; instead, it should be written as Version: 1.1. The presence of a space after the colon is essential for correct formatting.

2. When specifying the main class in the manifest file, refrain from adding the .class extension to the class name. Simply indicate the main class like this:

(An elaboration on the Main-Class section will be provided shortly.)

3. Make sure there is a newline character at the end of the manifest file. We can accomplish this by keeping the last line of the file blank. There is no necessity to explicitly write "\n" to signify a newline.

4. Additionally, it is essential to use UTF-8 encoding for the text file containing the manifest. Failure to do so may result in potential issues, emphasizing the significance of encoding considerations.

Example:

To modify the contents of our "HelloWorldApp.jar" archive and update the manifest file, we can use the following command:

In this case, the updated manifest file is named "Manifest.txt," and it contains the following contents:

Working with JAR and Manifest files In Java

Output:

Working with JAR and Manifest files In Java

Executable Jar Files

The jar command's -e option allows we to designate the entry point of our Java application. The entry point is typically the main class we want to execute when launching our program.

Example:

To generate a "HelloWorldApp.jar" file designating the "Client" class as the main class, we can utilize the jar command along with the -e option. An example command is as follows:

Output:

Working with JAR and Manifest files In Java

When designating a main class in the jar command using the -e option, it's important not to include the ".class" extension after the class name. Another approach involves adding a Main-Class entry directly to the manifest file and then updating it. In this scenario, we simply insert the following entry into the manifest file:

Once the main class is set, we can efficiently run a JAR program by using the following command:

Depending on the configuration of the operating system, users may find it convenient to start the application by double-clicking the JAR file icon. The system settings dictate this behavior, allowing users to launch the application without requiring command-line input.

Package Sealing

In Java, package sealing ensures that no additional classes can be added to a specific package. It becomes particularly relevant when utilizing package-visible classes, methods, and fields in our code. Without package sealing, the risk exists that other classes could insert themselves into the same package, thereby gaining access to features intended to be visible in the package.

  • To seal a package in Java, simply place all its classes into a JAR file.
  • JAR file packages are not sealed by default, but we can modify this default globally by adding specific lines to the manifest file.
  • Sealing a JAR file with `Sealed: true` in the manifest means locking down specific packages within that JAR. It ensures that classes in those packages come from the same source and prevents other JAR files from being modified or extended.
  • The package name must end with a "/".






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA