How to create an executable java jar file? I want to just double click the jar file and the application should run, please help me. I want to convert my java application to a windows exe file, how can I do it? These are two frequently asked questions.
For you guys who know about these things already you may skip this article, as there is no surprise element embedded. For us who are interested in learning new things go ahead, anything new adds up.
JDK gives us a tool to create jar files.
Before creating a java jar file, we need to create a java manifest file.
Command to create an executable java jar file:
jar -cvfm BundleExample.jar manifes
t.txt com/javapapers/corejava/*.class
Generally we will have the manifest file located in,
This manifest file contains information about the jar file. Information like which is the main java class to be executed, who created the application, version of the application, etc.
It is a regular text file. Remember to press enter at the end of each line and importantly in the last line.
So with respect to creating an executable java jar file, we need to add the main class of the application using which it can be launched. Our example manifest file is like below,
Main-Class: com.javapapers.corejava.JavaBundleExample
This is the only line I have and that is sufficient for our purpose.
We need a sample java application to enjoy this. Lets create a simple hello world type swing application.
package com.javapapers.corejava; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class JavaBundleExample { private static void createAndShowGUI() { JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("Bundle Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(new GridLayout(0, 1)); JButton button = new JButton("Dummy Button!"); pane.add(button); JLabel label = new JLabel("Example for Bundling JRE with Java Class"); pane.add(label); pane.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30)); frame.getContentPane().add(pane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Output:
Just execute the following line (command) to create the jar file, you should have added JDK to PATH.
jar -cvfm BundleExample.jar manifes
t.txt com/javapapers/corejava/*.class
Now we can double click the jar file and the application will launch and run.
If we create a windows native exe file, then we loose the platform independent-ness.
Let us use a tool called Launch4J. It’s a nice tool, works good and easy to use.
Its core features are,
Download Launch4J and use the GUI to create the windows/Java exe file. Launc4J creates an XML file to save the configuration.
Comments are closed for "Executable Java Jar File and Windows Exe File Creation".
Wonderful …. Nice article :-)
very nice article
thanks,, i am searching for it…
Nice and easy article,Thanks sir.
Kindly provide jar file creation process through the eclipse..
what about nsis?
nsis.sourceforge.net/Main_Page
Useful Information.Nice Article.
Thanks for sharing.
Another tool jsmooth.sourceforge.net
Thanks for Sharing..
Nice Tutorial.
Good to know for Developers.
I am not able to open the file.
when i am double clicking the file i am not getting any output.
Good Job.
Thanks sir
I have seen people spending hours and days to figure this out.. thanks for making it so clean and simple.
if we have to create a mark list program and to store a value to the back end. then how we can create a exe file for this back end process.
Thanks !!!
hello mr.joe,
iam new to this site after going through your site iam very pleased.
many thanks for spending your valuable time for people like me.
your thinking of giving preference to core java is great.
Thanks again from the bottom of my heart.
Regards,
Prashanth
Great article. I was under the impression that jar file to exe is not possible.
Thanks, I have doubt how to create .exe and how to show with icon. It’s great…..
Really its very useful to everyone as well as designed in nice….. Thank you….
Useful tutorial. Thanks for his. This method works. But what if I want to make an exe from a jar which is self-sufficient, i.e., does not need JRE installed. I searched a bi about this and found this: jnc.mtsystems.ch
Java native. The thing is I dont get how to operate it. Can you help?
Very useful article.. Thank you !!
Hi Joe,
Thanks for the information about Self extractable Jar file.Is it possible to create a Exe file,which contains the Self Extractable Zip.For example,I have a link to download EXE file.After downloading EXE,when i open,It should extract the Zip content and ask the user to select the store location.
I tried and googled lot,But i dont know how to implement this.Will you give some inputs or suggest me .Is there any way to implement this?
love… great.
Hi Joe,
Well I think I’ve tried everything. I reinstalled both Java and the JDK (with NetBeans), and that all works fine. I can run my Jar file in the Windows (7) command window by typing: java -jar myprog.jar
However, I want to be able to start my program by double-clicking it. I did “open with” and the browse and choose ….javabinjava.exe but it does nothing. I think that is because I can’t add the “-jar” part. Ugh!!
Again this works from the command line, with the -jar of course. I read on-line somewhere that I should edit the registry – I don’t know enough about that and don’t want to “mess up” anything, plus, it can’t be that hard!, I just want it to execute the jar when double-clicked. The computer “should” be smart enough to know ‘ “Oh, .jar file, I think I’ll run it with java -jar”.
Did everything given here but generated .exe file not able to execute… Help me
hi
iam chinna
welcome to this article
In Runnable Jar Part:
I have done the way it is said,
It Didn’t work initially, But
It started working after I added
Class-Path: . in the manifest file
This article has helped me. Simple and clearly explained. I have been searching for this content from long back
Launch4j is not useful at all,it just changes the extension nothing else
I created a .exe file but I’m not able to execute it help me out.
And will that exe run if d program needs arguments that are taken from command line argument
Dear sir ,
How to convert uhtm file into html file
that really good, thanks..
please provide tutorial on advance java.
Hi,
I was able to create executable. But when i launch it, the opened up application doesnt have any images. How can i resolve it?
Please help..
Hi,
I am able to create an .exe but after double clicking, it is not working in Windows.
In the class file, I am trying to get the input using “System.in”
Please let me know your comments.