Executable Java Jar File and Windows Exe File Creation

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.

How to Create an Executable JAR File?

JDK gives us a tool to create jar files.

jar_exe

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

How to create a java manifest file?

Generally we will have the manifest file located in,

  • META-INF/manifest.mf

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.

Sample Application

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:

sampleapplication

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.

How to Create a Windows Exe file?

If we create a windows native exe file, then we loose the platform independent-ness.

windows

Let us use a tool called Launch4J. It’s a nice tool, works good and easy to use.

Its core features are,

  • creating lightweight Windows native EXE
  • JRE version control
  • app icon
  • splash screen
  • process name
  • Java download page

Download Launch4J and use the GUI to create the windows/Java exe file. Launc4J creates an XML file to save the configuration.

launch4jconfig

  • I have specified the output file, this is the name of the exe file to be created.
  • Jar is the input file which we want to bundle as an exe file. Lets use the Java JAR which we created in the previous step.
  • In JRE tab, we can specify the Min JRE version required. So this will detect the JRE available in the system and verify if the version is sufficient.
  • Click the cog wheel icon from the menu to generate the exe file.

This Core Java tutorial was added on 07/04/2013.

Comments on "Executable Java Jar File and Windows Exe File Creation" Tutorial:

  1. Shailesh says:

    Wonderful …. Nice article :-)

  2. kalpesh says:

    very nice article

  3. vishwajeet nayak says:

    thanks,, i am searching for it…

  4. deepak says:

    Nice and easy article,Thanks sir.
    Kindly provide jar file creation process through the eclipse..

  5. Punit says:

    what about nsis?
    nsis.sourceforge.net/Main_Page

  6. pradeep says:

    Useful Information.Nice Article.
    Thanks for sharing.

  7. Sudheesh says:

    Another tool jsmooth.sourceforge.net

  8. Nishant says:

    Thanks for Sharing..
    Nice Tutorial.

  9. Anandan R says:

    Good to know for Developers.

  10. Padma Priya says:

    I am not able to open the file.

    when i am double clicking the file i am not getting any output.

  11. Suifeng says:

    Good Job.
    Thanks sir

  12. bharat says:

    I have seen people spending hours and days to figure this out.. thanks for making it so clean and simple.

  13. Anand.V says:

    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.

  14. Deepak Gakhar says:

    Thanks !!!

  15. prashanth says:

    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

  16. Jishnu L says:

    Great article. I was under the impression that jar file to exe is not possible.

  17. sushil kumar says:

    Thanks, I have doubt how to create .exe and how to show with icon. It’s great…..

  18. Ramesh kumar says:

    Really its very useful to everyone as well as designed in nice….. Thank you….

  19. Akashdeep Singh says:

    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?

  20. Deepak says:

    Very useful article.. Thank you !!

  21. Krishnaraj M says:

    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?

  22. arjndah says:

    love… great.

  23. Rob says:

    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”.

  24. Padda says:

    Did everything given here but generated .exe file not able to execute… Help me

  25. chinnadurai says:

    hi
    iam chinna
    welcome to this article

  26. Rohit says:

    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

  27. Rohit says:

    This article has helped me. Simple and clearly explained. I have been searching for this content from long back

  28. Soumalya Dutta says:

    Launch4j is not useful at all,it just changes the extension nothing else

  29. kaushik says:

    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

  30. Arun says:

    Dear sir ,
    How to convert uhtm file into html file

  31. om kesarwani says:

    that really good, thanks..

    please provide tutorial on advance java.

  32. remya says:

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

  33. Ujjwal says:

    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.

Your Comment

↑ Go to top