You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Java 11部署Swing/JavaFX桌面应用?求相关教程资源

Migrating Swing/JavaFX Desktop App from Java 8 Oracle JDK to Java 11 (Free JDKs) with Self-Contained Packaging

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.

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).

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.

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 the jmods folder in your Java 11 JDK.
  • Replace <path-to-javafx-jmods> with the jmods folder from your JavaFX SDK (skip this if using ZuluFX).
  • The --compress and --strip-debug flags 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:

  1. Configure Launch4J to point to your custom JRE’s bin/java.exe instead of a system-wide JRE.
  2. Package your app JARs + custom JRE into a single directory.
  3. 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 invoke jpackage to create installers.
  • Maven: Use the javafx-maven-plugin to run jlink, then invoke jpackage manually 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-modules flag for jlink.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:23:57