Home / Software Engineering / How to add a manifest file to jar file

How to add a manifest file to jar file

Print Friendly, PDF & Email

In the article showing how to run an application bundled in a jar file, I mentioned the simplest statement java -jar YourApp.jar  if your application was included a manifest file. Otherwise, the statement isn’t working. This post, therefore, presents how to add a manifest file to a jar file using either the typical way or Maven.

As far as manifest file introduced, it is normally used to define the following things:

  • Define the entry point of the Application, make the Jar executable.
  • Add project dependency classpath.

Presumably we have build a class named com.itersdesktop.javatechs.Demo. The following paragraphs will take this class as an illustrated example to ease the reading.

In a typical way

We want to execute the main method in the class Demo  in the package com.itersdesktop.javatechs  when we run the JAR file.

We first create a text file named Manifest.txt  with the following contents:

Main-Class: com.itersdesktop.javatechs.Demo Warning

We then create a JAR file named Demo.jar  by entering the following command:

jar cfm Demo.jar Manifest.txt com/itersdektop/javatechs/*.class

This creates the JAR file with a manifest with the following contents:

When you run the JAR file with the following command, the main method of Demo  executes:

java -jar Demo.jar

Setting an Entry Point with the JAR Tool

The ‘e’ flag (for ‘entry point’) creates or overrides the manifest’s Main-Class  attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class  attribute value in the manifest is set to Demo :

jar cfe app.jar Demo Demo.class

You can directly invoke this application by running the following command:

java -jar Demo.jar

If the entry point class name is in a package it may use a ‘.’ (dot) character as the delimiter. For example, if Main.class  is in a package called foo  the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class

With Maven

This section will help you familiarise with how to use the maven-jar-plugin  to create a manifest file in Maven based Java projects, and package/add it into the final jar file. When you run the command mvn package  to package project into a Jar, the following meta-inf/manifest.mf  file will be generated and added into the your Jar file automatically.

Define maven-jar-plugin  in pom.xml , and configure the manifest file via configuration tag. The basic syntax looks like

Notes

In Intellij IDEA, once clicking package function, we should see the result in the Run windows as shown in the following image.

For your references, you could take a look at the pom.xml content presented at the end of the post.

Hopefully you find interesting points once getting through my post. All comments are welcomed!

References

  1. https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
  2. https://maven.apache.org/plugins/maven-jar-plugin/
  3. https://www.mkyong.com/maven/how-to-create-a-manifest-file-with-maven/
  4. https://stackoverflow.com/questions/12767886/use-of-the-manifest-mf-file-in-java
comments

About Admin

programming all time and taking a bread for the lunch hahaha… When you are engaging your work, nobody can attract you doing another thing. Otherwise, should should move to other jobs or are not interested in your job enough.