How to make a Java exe file or executable JAR file

In this tutorial, we will show you how to create a executable JAR – When you double click on it, it runs the defined main class in manifest file.

1. AWT Example

Create a simple AWT Java application, just display label and print out some funny characters ~

AwtExample.java
package com.mkyong.awt;
 
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
public class AwtExample {
 
	public static void main(String[] args) {
 
		Frame f = new Frame();
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		f.add(new Label("This JAR file is executable!"));
		f.setSize(500, 500);
		f.setVisible(true);
	}
}

2. Manifest.txt

Create a manifest.txt file.

Manifest.txt
Main-Class: com.mkyong.awt.AwtExample

Uses Main-Class as the entry point of this Jar file, when you double click on this Jar file, the “AwtExample.class” main() method will be launched.

Note
Be sure that your manifest file ends with a new line, else your manifest file will not be parsed and failed to generate the manifest.mf. Read this http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html

Read this jar reference guide :
Be sure that any pre-existing manifest file that you use ends with a new line. The last line of a manifest file will not be parsed if it doesn’t end with a new line character.

3. Jar file

Create a Jar file by adding “AwtExample.class” and “manifest.txt” files together.

Assume your project folder structure as follows :

c:\test\classes\com\mkyong\awt\AwtExample.class
c:\test\classes\manifest.txt

You can issue following command to create a “AwtExample.jar.

jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class

Output

C:\test\classes>jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class
added manifest
adding: com/mkyong/awt/AwtExample$1.class(in = 638) (out= 388)(deflated 39%)
adding: com/mkyong/awt/AwtExample.class(in = 880) (out= 541)(deflated 38%)

4. Demo

Now, the “AwtExample.jar” is executable, double clicks on it, see the output :

make-jar-executable

References

  1. http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/jar.html
  2. http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
  3. http://www.mkyong.com/java/the-java-archive-tool-jar-examples/
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: Blue Coaster33()

  • Prashant Shirode

    Hello sir,
    I am develop one application for my company, and they need .exe file (not a.jar) file, so how i can do?

  • Prashant Shirode

    Hello sir,
    I need a help i am develop the one application for my company and i want to create .exe(not .jar) file, so how can i do??

  • http://www.dcmschools.com vishal

    hey i have create a mini project where an objective type question display..these question are being fetched from mysql database..
    by using your syntex for jar files
    jar -cvfm AwtExample.jar manifest.txt com/mkyong/awt/*.class

    i have successfully created jar file

    but not able to access the database questions etc

  • Manohar

    Thanks!

    Created my first jar file following your article!

  • mohammad

    thank you very much!

  • http://www.mapquest.com/places/majestic-collection-san-francisco-ca-11766751 Mahalia

    It is the best time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or advice.

    Maybe you can write next articles referring to this article.
    I desire to read more things about it!

  • PAYAL

    sir i am making a project on “internet download manager”.
    i have done coding in 4 parts and save them with diff naames in my system..
    now my problem is i have to make a jar file but i dont know how to combine all 4 progam in my jar file

  • http://theniggers.com bignigger5

    this textbox for comments should be at the top of the comments.

    I am having issues with trying to compile my main program via the command line, I am getting an error that says “cannot find symbol” a shitton of times. I think it’s because that java file requires other java files to operate and they’re not compiled yet, but to go and compile those one by one is tedious. The current method to compile these that I am using is javac *.java.

    Are there any solutions to this?

  • Madhaiyan

    Its working as expected,, Thank you so much…

  • Ram

    friends i m creating jar programmatically . help me i surf lot of stuff in internet via them
    i can create jar file but created jar not connected with ide. my concept is one program create jar with proper package structure that jar is going to used in other program.
    but in my case the jar what i created is not linked. if i try to add that as in classpath it
    not linked in programmes

  • Nishant Mishra

    Thanks for this but am getting following error
    I have copied full code to check but still giving the same
    Error: Could not find or load main class Hello

    Also in My code I am getting FileNotFoundException

    Please Help

  • Anubhav

    Hi I tried out this solution but my jar didnt run, the problem is in the jar, the jars that i had added in my project want not present in the newly built jar file so got the classNotFound exception. can anyone suggest a solution to it. Thanx in advance

  • venkateswara

    thank you posting this information

  • Pingback: How to create a jar file with Maven()

  • Pingback: The Java Archive Tool (JAR) Examples()

  • Hiral Jhaveri

    Thank you.

    The information turned out to be really useful for my Project.

  • Nick

    Thanks for the walk-through, was able to get my first .JAR working!

    One question though, the .jar wasn’t working with my main method as:

    public static void main(String args[])

    but when I changed around the brackets to:

    public static void main(String[] args)

    it worked. I thought these two were supposed to be equivalent?

  • AmitBhole

    Hi, Very easy to understand. But in my case its not working. If i double click on jar nothing happens.
    I’ll give you a short description about my task.
    I have created JAVA UI with WindowBuilder in Eclipse IDE. Then I have added Selenium scripts created using TestNG framework in Eclipse in same package that of java UI. I called selenium scripts on button click event. Now I am trying to create executable jar which can be executed on other machines also. By following your steps JAR is created, but on double clicking on it nothing happens.
    Folder structure is as below:
    C:\Selenium\Code\GUIForSelenium\bin\TestUI
    Code where I have created project
    GUIForSelenium is project
    TestUI is package
    TestUi folder contains all 3 class files 1 JAVA UI and 2 Selenium Code.

    Can you help in this issue?

  • Pingback: How to run .jar files without prompt, How?()

  • http://www.bennybriel.com Bennybriel

    Thanks alot. l did got some new thing here about jar files

  • Pingback: Can't seem to run a JAR file by cmd line or double-click.()

  • Pingback: Need a help on double-click jar file()

  • amin

    Thanks a lot for this really useful tutorial and it makes a lot of sense.But as i am a sort of beginner, I really need some help :(
    First of all, what exactly i have to do with the manifest file.What do i write in it?
    then If i have multiple class files how do i define them and i have to do all these things in cmd promt right?
    I have two classes here, a simple prime factor find program.One of them is main
    please tell me what else do i need to add in the code and what would be my manifest file in this case and also the commands for making them a runnable .jar file . please!

    import java.util.Scanner;
    public class TestDriver
    {
      public static void main(String[]args)
      {
        System.out.println("Enter a number from 2 to 100 to find the prime factors");
        Scanner s=new Scanner(System.in);
        int input=s.nextInt();
        factorcounter f=new factorcounter(input);
        System.out.println("Here are the prime factors: ");
        f.FindPrimeNo();
      }
    }
    public class factorcounter
    {
      public factorcounter(int a)
      {
        input=a;
      }
      public int input;
      public int temp;
      public int[]counter=new int[101];
      public void FindPrimeNo()
      {
        int judge;
        if(input%2==0)
        {
          judge=1;
        }
        else
        {
          judge=2;
        }
        switch (judge)
        {
          case 1:
            while(input>1)
          {
            if(input%2==0)
            {
              counter[2]++;
              input=(input/2);
            }
            else if(input%3==0)
            {
              counter[3]++;
              input=(input/3);
            }
            else if(input%5==0)
            {
              counter[5]++;
              input=(input/5);
            }
            else if(input%7==0)
            {
              counter[7]++;
              input=(input/7);
            }
            else
            {
              counter[input]++;
              input=1;
            }
          }
          case 2:
            while(input>1)
          {
            if(input%3==0)
            {
              counter[3]++;
              input=(input/3);
            }
            else if(input%5==0)
            {
              counter[5]++;
              input=(input/5);
            }
            else if(input%7==0)
            {
              counter[7]++;
              input=(input/7);
            }
            else
            {
              counter[input]++;
              input=1;
            }
          }
            for(int i=1;i<100;i++)
            {
              if(counter[i]!=0)
              {
                System.out.println(i+" occured "+counter[i]+" time");
              }    
            }
        }
      }
    }
  • anup

    I have created executable jar TestDS32.jar which uses some external Axis jars.
    Required jars path and main class name are set in the manifest file.
    My manifest file is look like :

    Main-Class: test.TestDS32
    Class-Path: TestDS32.jar D:\Axis\lib\log4j-1.2.8.jar D:\Axis\lib\axis-ant.jar D:\Axis\lib\axis.jar D:\Axis\lib\commons-discovery-0.2.jar D:\Axis\lib\commons-logging-1.0.4.jar D:\Axis\lib\jaxrpc.jar D:\Axis\lib\saaj.jar D:\Axis\lib\wsdl4j-1.5.1.jar

    But while executing TestDS32.jar from cmd by command java -jar TestDS32.jar i got below error :

    Exception in thread “main” java.lang.NoClassDefFoundError: \test\TestDS32
    Caused by: java.lang.ClassNotFoundException: \test\TestDS32
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    Could not find the main class: \test\TestDS32 . Program will exit.

    can you please tell me why this error is coming and how to solve it?

  • Namrata Kamble

    thankx thats realy easy to Understand
    but don’t know why my jar file is not Running.(Executing)
    (when i m going to Double click on my jar File, its not Working)
    :(

    • http://www.mkyong.com mkyong

      Can you show me your code and mainfest?

      • amin

        sir,
        i had a problem life time ago
        but still no reply
        :(
        i would still love to have a reply……

      • Namrata Kamble

        Actuly Sir, My Code is So large. its not easy to Post over here.
        in my Code there are 2 Parent class & many child classes.
        having many classes in my code,is that really Creating any Problem for my JAR file.

      • santosh

        hi mkyoung,
        This is santosh.i have one problem with generating executable jar. in my project i have have some .clas files ,two .dll files and one jar file. i want to make these as executable jar. could you help me how to do?
        Thanks in advance.

  • novo

    Than you!!! simple and easy :)))

  • Srinivas Charan

    Thank u very much, excellent content.great job

  • Pingback: Creating executable jar file in Eclipse - Java Forums()