Main Tutorials

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. https://mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
  3. https://mkyong.com/java/the-java-archive-tool-jar-examples/

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
43 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
bharath
10 years ago

i need a video tutorials fr this

Nishant Mishra
11 years ago

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

Ajay J
3 years ago

Hello I create Java swing app with database MySql. I created .exe file using launch4j as well advancedinstaller. Running jar file is working perfectly but after installing exe the app opens but wont work after providing credentials. It seems it is not able to connect or verify with DB. Can you suggest what could be the problem

vipul
4 years ago

Please tell me how to add database file with jar

Krishna
6 years ago

How to include static content like html pages?
I have an application where I start jetty in embedded mode and include some web files. but when created executable jar (I tried maven-assembly-plugin ) it is NOT including web content. Is it possible?
So in a nut shell, creating an executable WAR (or JAR) that serves html pages too.

panneer
7 years ago

how to create .exe file by using java code ..?

Sourav Dey
8 years ago

Hi Sir,

can anyone tell me how to create .jar file from .class file in java programmatically?

Javad
9 years ago

Thank you!
It was simple, easy and useful.

karthik
9 years ago

hai, i create exe jar file but it could not work i need some help…………………….

Mukesh Saini
9 years ago

How to run jar file double click on it in jdk 7 Please sir tell me

Shashi
9 years ago

Hi Mkyong,

I have created a spring-hibernate standalone app ( NOT web app). It’s running successfully in eclipse but when i create a runnable jar and run it from terminal using “java -jar jarname.jar” then it is running and printing the println statements but its not able to find the beans declared in applicationContext file and saying :
“Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘lbFileUploadService’ is defined”

i have done something like this in the main class:

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(“classpath*:**/applicationContext*.xml”);

NOTE: Its running successfully in eclipse.

I am not sure but i think it’s not able to read “applicationContext.xml”
And i have to create a jar out of this project and configure into cron job.

Please suggest me ASAP.
Thanks

Andaluz
10 years ago

If you use Eclipse, you could also rightclick on the project -> export -> Jar file. Than you can run the jar file like this: java -jar your.jar

santosh varma
10 years ago

could anyone tell me, what is the difference between technology and programming language?
Thanks in advance…

Mr.Robot
7 years ago
Reply to  santosh varma

@santoshvarma:disqus

With such a question, I think you are in the wrong place

Prashant Shirode
10 years ago

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
10 years ago

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??

Suryaprakash
8 years ago
bharath
10 years ago

u may need izpack

vishal
10 years ago

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

mohammad
10 years ago

thank you very much!

Mahalia
10 years ago

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
10 years ago

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

bignigger5
10 years ago

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
10 years ago

Its working as expected,, Thank you so much…

Ram
11 years ago

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

Anubhav
11 years ago

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
11 years ago

thank you posting this information

Hiral Jhaveri
11 years ago

Thank you.

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

Nick
11 years ago

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
11 years ago

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?

Bennybriel
11 years ago

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

amin
12 years ago

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");
          }    
        }
    }
  }
}