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 ~
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.
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.
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.htmlRead 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 :

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

Its working as expected,, Thank you so much…
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
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
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
thank you posting this information
Thank you.
The information turned out to be really useful for my Project.
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:
but when I changed around the brackets to:
it worked. I thought these two were supposed to be equivalent?
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?
Thanks alot. l did got some new thing here about jar files
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!
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?
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)
:(
Can you show me your code and mainfest?
sir,
i had a problem life time ago
but still no reply
:(
i would still love to have a reply……
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.
Than you!!! simple and easy :)))
Thank u very much, excellent content.great job