How to create a Windows Native Java application (generating .exe file)?

Let’s say you have created a Java application and packaged it into a jar.

You want to install this jar in another machine.

Traditionally , you need to first install java in that machine and then copy the jar . The application can be launched by running the jar.

Or you can use third party applications to convert the java application to an exe installation. Java itself didn’t provide this facility for a long time.

Java 8 came up with jpackage tool which can be used to create .exe files. These files can then be used to install a native Windows Application. No need to install Java in the client machine anymore!

Java again removed the tool in Java 11 version and brought it back in Java 14.

Let’s create a simple Java application which displays the message “Hello World” in a message box and convert it into an exe file. Then let’s install the application in a Windows machine.

Here is the algorithm:

STEP 1: Create the java application

STEP 2: Use ‘jar’ tool to generate a jar file

STEP 3: Use ‘jpackage’ tool to convert the jar file to exe

STEP 4: Install the exe file on a Windows machine

STEP 1: Create the java application :

I created a Java application with a single file which opens up a message box with the message “Hello World”.

Here is the code:

package com.helloworld;

import javax.swing.*;

public class Main {

    public static void main(String[] args) {


        JOptionPane.showMessageDialog(null, "Hello World", "A Windows Application", JOptionPane.INFORMATION_MESSAGE);

    }
}

On running this , I get the below output:

Now let’s convert this project into a jar file

STEP 2: Use ‘jar’ tool to generate a jar file

Below is the path where I have created the Java application:

G:\projects\fullstackdeveloperblog\spring\springboot\HelloWorld\

On building the project the classes got generated in out/production folder ( I am using IntelliJ):

G:\projects\fullstackdeveloperblog\spring\springboot\HelloWorld\out\production

A new folder named HelloWorld got generated in the above directory.

I navigated inside it and ran the below jar command:

jar cfm helloworld.jar manifest.txt com

helloworld.jar is the name of the jar file to generate

manifest.txt contains meta data about the jar (what is the path of the main class etc)

com is the directory (inside HelloWorld folder) which contains the Main class

To know more about generating jar files refer here : (Generating jar)

A jar file got generated as below:

If you double click the jar file you get the same output:

STEP 3: Use ‘jpackage’ tool to convert the jar file to exe

As a prerequisite to run jpackage tool to generate a exe file , you need to install Wix Toolset (suggested by Java) from here .

In the same path where I ran ‘jar’ command I ran the below jpackage command:

jpackage --name helloworld --input . --main-jar helloworld.jar --main-class com.helloworld.Main --win-console

Following –name is the name of the exe file

Following –input (.) is the name of the directory which contains the jar file and other inputs (in this case the current directory)

Following –main-jar is the name of the jar which contains the class to be run

Following –main-class is the name of the class which is to be run.

–win-console means windows console need to be enabled – Command line and Java swing applications need this to be enabled.

–win-console flag is quite important. It took me a while to realise this was needed (Else application won’t be launched when you click on the installed exe)

Here is the screenshot of running the jar and jpackage commands:

Below is the exe file generated:

STEP 4: Install the exe file on a Windows machine

Now let’s install the application using the exe file created.

Double click on helloworld-1.0.exe.

The application gets installed :

It gets installed in the default C:/Program Files directory:

Go into the folder and click on helloworld.exe:

Hello World message is shown!

Now let’s try to execute this exe file through command prompt from any path in Windows machine.

For this to happen , add the path of the exe to the ‘PATH’ environment variable of your machine:

Now go to any path in the command prompt and enter helloworld:

We have created a native windows application!

As said earlier by default it gets installed in C:/Program Files directory .To prompt the user to provide the path while installation add the flag –win-dir-chooser to jpackage command:

jpackage --name helloworld --input . --main-jar helloworld.jar --main-class com.helloworld.Main --win-console --win-dir-chooser

If you want to create a short cut in the desktop once your application is installed add the flag –win-shortcut to the above command:

jpackage --name helloworld --input . --main-jar helloworld.jar --main-class com.helloworld.Main --win-console --win-dir-chooser --win-shortcut

If you want to create a menu under the Start Menu add two flags –win-menu –win-menu-group and against –win-menu-group provide the name of the menu you want:

jpackage --name helloworld --input . --main-jar helloworld.jar --main-class com.helloworld.Main --win-console --win-dir-chooser --win-shortcut --win-menu --win-menu-group "Hello World"

If you want to provide an icon for your application , use –icon flag. Copy the icon file in the same directory where you are running jpackage command.

jpackage --name helloworld --input . --main-jar helloworld.jar --main-class com.helloworld.Main --win-console --win-dir-chooser --win-shortcut --win-menu --win-menu-group "Hello World" --icon helloworldicon.ico

Let’s run jpackage tool with all the above options and create an exe installation file.

Now if you run the generated exe file it will prompt you for installation directory:

Once successfully installed , go to your desktop and notice a short cut created with the icon you chose. Double click the file and Hello World message gets displayed!

Go to start menu and notice the app “Hello World”:

Creating a Windows Native Application in Java got easier and cooler.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s