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 --version jlink --version
mvn --version
npx --version
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) {
= new JLabel("Hello, World!");
var label .setFont(new Font("Serif", Font.PLAIN, 72));
label
= new JButton("Uppercase");
var uppercaseButton .addActionListener(e ->
uppercaseButton.setText(WordUtils.capitalize(label.getText()))
label);
= new JButton("lowercase");
var lowercaseButton .addActionListener(e ->
lowercaseButton.setText(WordUtils.uncapitalize(label.getText()))
label);
= new JPanel();
var panel .setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(label);
panel.add(uppercaseButton);
panel.add(lowercaseButton);
panel
= new JFrame("Basic Program");
var frame .add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame}
}
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"
< xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
modelVersion>4.0.0</modelVersion>
<
<groupId>example</groupId>
<artifactId>javaexe</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
manifestEntries>
<<Main-Class>example.Main</Main-Class>
<Build-Number>1.0</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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
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.
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
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.