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/

About the Author

author image
mkyong
Founder of Mkyong.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

Leave a Reply

avatar
newest oldest most voted
Krishna
Guest
Krishna

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
Guest
panneer

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

Sourav Dey
Guest
Sourav Dey

Hi Sir,

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

Javad
Guest
Javad

Thank you!
It was simple, easy and useful.

karthik
Guest
karthik

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

Mukesh Saini
Guest
Mukesh Saini

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

Shashi
Guest
Shashi
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… Read more »
Andaluz
Guest
Andaluz

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

bharath
Guest
bharath

i need a video tutorials fr this

Steve
Guest
Steve
santosh varma
Guest
santosh varma

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

Mr.Robot
Guest
Mr.Robot

@santoshvarma:disqus

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

Prashant Shirode
Guest
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
Guest
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??

bharath
Guest
bharath

u may need izpack

Suryaprakash
Guest
Suryaprakash
vishal
Guest
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
Guest
Manohar

Thanks!

Created my first jar file following your article!

mohammad
Guest
mohammad

thank you very much!

Mahalia
Guest
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
Guest
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

bignigger5
Guest
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
Guest
Madhaiyan

Its working as expected,, Thank you so much…

Ram
Guest
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
Guest
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
Guest
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
Guest
venkateswara

thank you posting this information

trackback
How to create a jar file with Maven

[…] make jar file like a exe file, you need to define a manifest file and declare the application entry point inside via […]

trackback
The Java Archive Tool (JAR) Examples

[…] P.S More detail… […]

Hiral Jhaveri
Guest
Hiral Jhaveri

Thank you.

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

Nick
Guest
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
Guest
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… Read more »
trackback
How to run .jar files without prompt, How?

[…] {ord=Math.random()*10000000000000000;}document.write(''); See the example here: How to make an executable JAR file Website: Learn Java Programming by Examples Reply With Quote « […]