JavaFX or Swing + jpackage + Maven template project for generating native desktop applications.
- Build nice, small cross-platform JavaFX or Swing desktop apps with native installers
- Apx 30-40mb .dmg, .msi and .deb installers - check out the example builds in releases.
- Just use Maven - no shell scripts required.
- Use standard Maven dependency system to manage dependencies
- Generate macOS (.dmg), Windows (.msi) and Unix (e.g. deb/rpm) installers/packages automatically with GitHub Actions.
This template uses a Maven plugin to generate a custom JVM and installer package for a JavaFX application. It can easily be adapted to work with Swing instead.
- Java 16 and Maven.
- On macOS XCode is required.
- On Windows the free WiX Toolset is required.
The project includes GitHub Actions which automatically generate macOS, Windows, and Linux installers.
The generated installers come in at around 30-40mb. The example source in the project includes demonstrations of several native desktop features - for example, drag-and-drop from the Finder/Explorer, as well as a few macOS Dock integration examples. Removing the code and the demonstration dependendencies gets a "Hello World" build size closer to 30mb than 40mb.
Here are few cool things in this template:
- Only uses Java and Maven. No shell scripts required.
- Includes sample GitHub Actions to build macOS, Windows and Linux installers
- Demonstrates setting the application icon
- Builds a .dmg on macOS, .msi on Windows, and .deb on Linux
- Bundles the JavaFX SDK & modules to simplify getting started.
- Just delete the JavaFX stuff if you are using Swing
- Template includes several examples of JavaFX / native desktop integration
- Drag & drop with Finder / Explorer
- Change the Dock icon dynamically on macOS
- Menu on the top for macOS, in the window itself on Windows
- Request user attention (bouncing dock icon) on macOS
- Java + Java modules are used to build a trimmed JVM (a few thoughts on Java modules)
- The user application uses ordinary Maven dependencies and classpath to run the application
- Nice illustration of how to use jlink to build a slim JVM, point jpackage at that JVM and still use the ordinary classpath for the application
Once you get started, you might find these lists of tutorials, tools, libraries for JavaFX and general Java desktop integration helpful.
tl;dr absolutely.
It's actually a lot easier to use Swing instead of JavaFX, as Swing is built in to the JDK and doesn't require fiddling with Java modules.
Just delete the JavaFX stuff, including the JavaFX modules and add a Swing main class instead. If you are reasonably familiar with Maven this shouldn't be very hard to do.
I highly recommend the FlatLaf as a must use with Swing. That look-and-feel plus designers such as the IntelliJ GUI Designer or JFormDesigner can work very well, arguably with an easier learning curve than JavaFX.
In particular, delete the following directories:
/linux-javafx
/mac-javafx
/win-javafx
Changes to the pom.xml:
- Remove the javafx modules from the jvm.modules property
- Remove the javafx.version property.
- Remove the three org.openjfx dependencies
- Remove the configuration/excludeGroupIds section from the maven-dependency-plugin
- Remove javafx-maven-plugin from the plugins list
- Remove the modulePath delcaration from the jtoolprovider-plugin execution/configuration
Once everything is installed (see below) it's really easy to use:
To generate an installer, just run...
mvn clean install
To do everything up until the actual installer generation (including generating the custom JVM)...
mvn clean package
- Install OpenJDK Java 16 or
Oracle Java 16.
- Verify by opening a fresh Terminal/Command Prompt and typing
java --version
.
- Verify by opening a fresh Terminal/Command Prompt and typing
- Install Apache Maven 3.6.3 or later and make sure it's on your path.
- Verify this by opening a fresh Terminal/Command Prompt and typing
mvn --version
.
- Verify this by opening a fresh Terminal/Command Prompt and typing
- macOS: verify XCode is installed and needed agreements accepted.
- Launch XCode and accept the license, or verify in Terminal with the command
sudo xcodebuild -license
.
- Launch XCode and accept the license, or verify in Terminal with the command
- Windows: install Wix 3 binaries.
- Installing Wix via the installer should be sufficient for jpackage to find it.
- Clone/download this project.
- Final step: run
mvn clean install
from the root of the project to generate thetarget\TestApp.dmg
ortarget\TestApp.msi
(installer).- Note that the actual generated installer will include a version number in the file name
- For reference, here is a complete run log for a successful run.
Because these builds use stripped down JVM images, the generated installers are in the 30-40mb range.
- If the built app fails to run, make sure the JavaFX app runs as expected first by using the
mvn javafx:run
command. This will run the app in development mode locally, and you should see standard System.out debug lines appear in your console.- Many flavors of Linux fail to run here for a variety of reasons. Head over to the discussions or perhaps consider your consulting budget or a JavaFX support contract from Gluon.
- Check the Maven build logs (of course).
- By default, the app will generate debug*****.log files containing the output from System.out. You can look at the
main method of
BaseApplication.java
to see how this is done. For a production app, you would want to place these logs in the correct OS specific location. On a Unix machine you cantail -f
the log normally.
There are a LOT of different flavors of Linux out there. I've provided the Linux build more as an example of how the GitHub Action works, but I can't diagnose or trouble-shoot your Linux build (unless it's a consulting engagement). Feel free to post these in discussions!
I will note, however, that much of the Linux trouble I have seen comes from some of the included integration
demonstrations. Try commenting out the loading of demo plugins in BaseApplication.java
- specifically the loop that
loads the plugins.
In theory, the Exception handler in the plugin loader code should catch the exceptions. In practice, on a few flavors of Linux something dies with a native exception that takes it all down.
I get more support/issues for Linux builds than anything else, often for distros I've never heard of... which is cool but not something I'm really set up to deal with (short of paid consulting). That said, every Linux support issue so far has been resolved pretty easily by folks posting Maven or application log files in the discussion group. No promises, but go forth and post!
Here is the checklist for M1 support for this template:
- Installing JDK 17
- Currently EA
- Installing the matching JavaFX 17 jmods
- Currently EA
- Configuring the build to detect the correct OS + architecture mix.
- Instead of Maven detect "mac" or "linux" the Maven builds now need to be architecture aware.
- Probably fine for a local M1 build
- Configuring GitHub Actions to run the build
Oh, and I currently don't have an M1 Mac (waiting for the 15" MBP, ahem). Once I get that machine I'll likely start with a Java 17 + JavaFX 17 branch.
That said, there is no reason not to try it all out locally if you have an M1 Mac (or ARM Linux)! Feel free to post to the discussion board if you are working on either of these.
Java 15 will work, although it requires a bit of setup. Honestly I can't think of a single reason someone would stick with Java 15 instead of 16 (or 17), so I'll likely delete this soon.
Problems? Make sure everything is installed and working right!
- Compiler not recognizing the --release option? Probably on an old JDK.
- Can't find jdeps? Probably on an old JDK.
- Can't find jpackage on Java 15? Probably haven't set up your system to allow Java 15 to enable preview packages.
- Unrecognized option: --add-modules jdk.incubator.jpackage
- Could be a left-over MAVEN_OPTS setting when you switched from Java 15 to Java 16
- If you are still on Java 15, you may not have MAVEN_OPTS set correctly.
If you need consulting support, feel free to reach out at ChangeNode.com.
If you are using the template, browsing the Q&A is highly recommended.