CodingJump border
Create Executable Jar File
Published on

Create Executable Jar File


When you try to run the JAR file with below command, lets assume that the JAR file is named as example-0.0.1.jar.

java -jar example-0.0.1.jar

no main manifest attribute, in example-0.0.1.jar

Let's extract the contents of the JAR file as this is just a ZIP file with below command. You may see some output like below as every project is quite different.

unzip example-0.0.1.jar

Archive:  example-0.0.1.jar
   creating: META-INF/
  inflating: META-INF/MANIFEST.MF    
   creating: META-INF/maven/
   creating: META-INF/maven/com.codingjump/
   creating: META-INF/maven/com.codingjump/hello-world/
  inflating: HelloWorld.class        
  inflating: META-INF/maven/com.codingjump/hello-world/pom.xml  
  inflating: META-INF/maven/com.codingjump/hello-world/  

To fix this we need to add a manifest file which points to the Main class. We can make use of the maven-jar-plugin for adding META-INF/MANIFEST.MF file so that when we run the JAR directly it works.

Currently it is almost blank.

Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.3.0
Build-Jdk-Spec: 20

Now lets use the maven-jar-plugin and define the mainClass as below.


Now if you build your JAR file should have following contents in META-INF/MANIFEST.MF

Manifest-Version: 1.0
Created-By: Apache Maven 3.9.4
Built-By: snrahul11
Build-Jdk: 20.0.2
Main-Class: HelloWorld

Now we have a attribute called Main-Class which defines our starting point for JAR file. Now if you run the JAR file you should see some output on the console.

java -jar example-0.0.1.jar

Hello World!!!

Similarly we can make use of spring-boot-maven-plugin with the below syntax.

          <mainClass> com.codingjump.Program</mainClass>

It works quite similar to the maven-jar-plugin. You can follow this post if you want to learn more about Spring Boot Command Line Application.


Hope this has gives you some basic understanding of how you can create a executable JAR file. There are many things which we need to learn about JAR packaging, etc. But I believe this is enough to give you a head start towards Java Development.