如何基于Java 11部署Swing/JavaFX桌面应用?求相关教程资源
Let's break down how to adapt your deployment workflow for Java 11, using free JDKs like Amazon Corretto, Eclipse Temurin (formerly AdoptOpenJDK), or Azul Zulu, and replace your old Launch4J+NSIS setup with modern self-contained packaging.
First: Pick Your Java 11 JDK
All three options you mentioned are solid, production-ready choices:
- Amazon Corretto: AWS-backed, long-term support, with straightforward downloads for all major platforms.
- Eclipse Temurin: Maintained by the Eclipse Foundation, with a focus on open-source compliance and stability.
- Azul Zulu: Offers pre-bundled JavaFX builds (called ZuluFX), which saves you from downloading and configuring JavaFX separately.
For your Swing+JavaFX app, I’d recommend ZuluFX if you want to skip manual JavaFX setup—otherwise, download the standalone JavaFX SDK for your chosen JDK.
Core Approach: Custom JRE with jlink + Native Installers
Java 11 removes the standalone JRE, but jlink lets you create a minimal custom JRE containing only the modules your app needs. You can then pair this with tools to build native installers (replacing your old Launch4J+NSIS workflow).
Step 1: Prepare Your App for Modularization (Optional but Recommended)
If your app isn’t already modular, add a module-info.java file to your project root to define dependencies:
module com.your.app.jstock { requires java.desktop; // Required for Swing components requires javafx.controls; // If using JavaFX UI elements requires javafx.fxml; // If your app uses FXML layouts exports com.your.app.main; // Export the package containing your main class }
If you don’t want to modularize, you can still use jlink with the --add-modules flag to include required modules manually.
Step 2: Build a Custom JRE with jlink
jlink trims down the full JDK to only the modules your app needs, reducing distribution size. Run this command (adjust paths and modules to match your app):
jlink --module-path "<path-to-jdk-jmods>;<path-to-javafx-jmods>" \ --add-modules java.desktop,javafx.controls,javafx.fxml,com.your.app.jstock \ --output custom-jre \ --strip-debug \ --compress=2 \ --no-header-files \ --no-man-pages
- Replace
<path-to-jdk-jmods>with thejmodsfolder in your Java 11 JDK. - Replace
<path-to-javafx-jmods>with thejmodsfolder from your JavaFX SDK (skip this if using ZuluFX). - The
--compressand--strip-debugflags shrink the custom JRE to 50-100MB (a huge improvement over the full Oracle JRE).
Step 3: Create Native Installers
You have three flexible options here, depending on your familiarity with tools:
Option A: Use jpackage (Official, Recommended)
jpackage is the official tool for building native installers. Note: Java 11 doesn’t include jpackage by default, but you can download a standalone backport version from OpenJDK community builds.
Run this command to build a Windows EXE installer:
jpackage --name JStock \ --input ./path/to/your/app/jars \ --main-jar jstock-main.jar \ --runtime-image ./custom-jre \ --type exe \ --icon ./path/to/jstock-icon.ico \ --install-dir "Program Files/JStock"
This generates an EXE that bundles your custom JRE and app files—users won’t need to download any JRE separately, eliminating Oracle licensing headaches.
Option B: Stick with Launch4J + NSIS (Legacy Workflow)
If you prefer your existing tools, you can adapt them:
- Configure Launch4J to point to your custom JRE’s
bin/java.exeinstead of a system-wide JRE. - Package your app JARs + custom JRE into a single directory.
- Use NSIS to build an installer that copies this directory to the user’s machine and creates desktop shortcuts.
Option C: Build Tool Plugins (Automate Workflow)
For Maven/Gradle users, plugins handle jlink and packaging automatically:
- Gradle: Use the
badass-jlink-plugin(part of the OpenJFX ecosystem). It detects module dependencies, builds the custom JRE, and can invokejpackageto create installers. - Maven: Use the
javafx-maven-pluginto runjlink, then invokejpackagemanually or via a supplementary plugin.
Key Notes for Swing/JavaFX Apps
- JavaFX Dependencies: Double-check that all JavaFX modules your app uses are included in the
--add-modulesflag forjlink. - Cross-Platform: All tools work on Windows, macOS, and Linux—you’ll need to run the commands on the target OS, or use
jpackage’s cross-compilation flags. - Size Optimization:
jlink’s compression flags can cut your custom JRE size by 50% or more, making downloads faster for users.
内容的提问来源于stack exchange,提问作者Cheok Yan Cheng

