Turn any Java program into a self-contained EXE

by: Ethan McCue

Double-click to run is one of the easiest ways to open a program.

If the person you are sharing code with already has the right version of Java installed, they can double-click on a jar file to run it. You wrote it once, they can run it there.

If they don't have Java installed, then there are ways to create a runnable installer like jpackage, but now they have to click through an installer to be able to run your code.

You can use Native Image to turn your code into an exe which won't require them to have anything installed, but now you have to abide by the closed world assumption and that's not always easy or possible.

So this post is going to focus on a fairly oonga boonga approach that will work for any app, regardless of what dependencies you include or JVM features you make use of.

The code along with an example GitHub workflow can be found in this repo and final executables can be found here.


Java 9+

java --version
jlink --version


mvn --version


npx --version

Step 1. Compile and Package your code into a jar.

This toy program will create a basic window that has some text that you can toggle between being capitalized.

package example;

import org.apache.commons.text.WordUtils;

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        var label = new JLabel("Hello, World!");
        label.setFont(new Font("Serif", Font.PLAIN, 72));

        var uppercaseButton = new JButton("Uppercase");
        uppercaseButton.addActionListener(e ->

        var lowercaseButton = new JButton("lowercase");
        lowercaseButton.addActionListener(e ->

        var panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        var frame = new JFrame("Basic Program");

Program Demonstration

The goal is to package up your code, along with its dependencies, into a jar. Jars are just zip files with a little extra structure.

For a Maven project the configuration will look like the following.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">




                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

Where the "shade" plugin will handle including the code from all of your dependencies into the jar. In this case, the only external dependency is org.apache.commons/commons-text.

mvn clean package

Then for the purposes of this guide we will move that jar into a new directory where it will be separate from whatever other files are in target/.

mkdir build 
mv target/javaexe-1.0.jar build

Step 2. Create a Java Runtime Environment

In order to run the jar from the previous step, we will need to bundle it with a Java Runtime Environment. To do this we will use jlink.

Since the Java ecosystem hasn't embraced modules, you most likely haven't heard of or used jlink.

The short pitch is that it can create "custom runtime images." Say you are making a web server. You don't need AWT or Swing, so including all the code for that is a tad wasteful. With jlink you can make a JRE that doesn't include the java.desktop module at all.

This system works best if your application and all of its dependencies include compiled module-info.java files which let jlink know exactly what modules you want to include. You can also manually figure out the list of required modules by using jdeps and a bit of detective work.

Even without a modular project though, we can still use jlink to effectively clone our Java installation to a directory.

jlink --add-modules ALL-MODULE-PATH --output build/runtime

Including every module gives confidence that libraries like org.apache.commons/commons-text will work as intended, even though we never figured out what modules they actually require.

Step 3. Bundle the Jar and the JRE into an executable

So with a jar containing our code and all of its dependencies in one hand and a JRE in the other, all that's left is to stitch the two together.

The general technique for that is to

  1. Zip up the directory containing the JRE and your application jar.
  2. Attach a stub script to the top of that zip file which will extract the zip to a temporary directory and run the code.

There is a JavaScript library which does this called caxa. Its purpose is making NodeJS projects into executables, so it will also bundle whatever NodeJS installation is on the system. That step can luckily be skipped by passing the --no-include-node flag, so it will work just fine for this.

npx caxa \
    --input build \
    --output application \
    --no-include-node \
    -- "{{caxa}}/runtime/bin/java" "-jar" "{{caxa}}/javaexe-1.0.jar"

This will create an executable called "application." If you are doing this for Windows you should specify "application.exe." When the executable is run the {{caxa}}s in the command will be substituted for to the temporary directory where the zip file was expanded.

I am aware of jdeploy - and it does handle stuff that I didn't cover or would be relatively hard with this scheme like code signing or automatic updates - but as far as I can tell it still requires that users run an installer.

On code signing, there is an open issue with caxa to figure out how to do that. I can make another post or update this one if an approach is figured out. I don't quite understand the issue, so I don't feel qualified to comment.

If any mildly ambitious reader wants to try their hand at making caxa in a different language so this process isn't dependent on the JS ecosystem I encourage it.

As always, comments and corrections welcome.

<- Index