How to make an executable JAR file
In this tutorial, we show you how to create a JAR and make it executable by double clicks on it.
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 text file. Main-Class defines the entry point of this Jar file, when you double clicks on this Jar file, the “AwtExample.class” main() method will be launched.
File : Manifest.txt – Only one statement and end with a new line character (double clicks at the end of the statement to create a new line)
Main-Class: com.mkyong.awt.AwtExample
Be sure that your manifest file ends with a new line, else your manifest file content will not be parsed and append to the system generated manifest.mf.
You can read this manifest.mf reference guide, and look for the following statement.
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 following :
c:\test\classes\com\mkyong\awt\AwtExample.class c:\test\classes\manifest.txt
You can issue this command to create a “AwtExample.jar” file.
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 output :








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?
Than you!!! simple and easy :)))
Thank u very much, excellent content.great job
[...] Read the conditions mentioned by pbrockway2 above. This link will probably clear all you doubts : How To Make An Executable Jar File. Google is probably the best thing, that has happened to us. Please utilize it properly. Hope [...]