How to Create an Executable JAR File in Java

by xanthium-enterprises in Circuits > Software

2950 Views, 0 Favorites, 0 Comments

How to Create an Executable JAR File in Java

How to create an executable Java JAR file using JDK /GraalVM command line tools

In this Instructable ,You will learn how to create an executable jar file using JDK tools.

The Instructable is aimed at beginners who are new to Java

The Code is written in Java and done on a Windows 10 Computer



Supplies

create-exe-jar-file-jdk.png

This is a software based project so you don't need any supplies. You will have to download a few software's

  • Java Development Kit
  • Text Editor

Creating a Basic Java Jar Executable File

creating-executable-jar-file-java-graalvm-jdk.jpg

To create an executable JAR file you will have to tell the entry point of the program or which class contains the main() function.

This can be done by the following option.

jar cvfe name-of-your-jar.jar ClassHavingMainFunc  Location\your\myclass.class


For example,consider a simple code like HelloWorld.java

public class HelloWorld 
{
   public static void main (String[] Args)
   {
     System.out.println("HelloWorld");
    }
}

Place it inside a directory and compile the source code to .class file .

Now create the executable JAR file using the below command 

jar cvfe HelloJar.jar HelloWorld HelloWorld.class

You can then run the resulting jar file by 

java -jar HelloJar.jar

Now if you unzip the JAR file and view its MANIFEST.MF file

Manifest-Version: 1.0
Created-By: 17.0.2 (GraalVM Community)
Main-Class: HelloWorld