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

Maven构建Swing集成JavaFX应用运行时缺失SwingInterOpUtils问题

Swing集成JavaFX构建Fat Jar报错解决方案

错误原因

java.lang.NoClassDefFoundError: jdk/swing/interop/SwingInterOpUtils报错是因为JavaFX 16的Swing集成组件依赖JDK的jdk.unsupported.desktop模块,该模块不属于标准JRE组成部分,也不会被maven-shade-plugin默认纳入Fat Jar。

可行解决方案

方案1:修改打包配置,Manifest内置模块参数

在maven-shade-plugin的ManifestResourceTransformer配置中添加模块启动参数,打包后运行时自动生效,无需额外指定启动命令:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <mainClass>org.openjfx.hellofx.AppRun</mainClass>
    <manifestEntries>
        <Add-Modules>jdk.unsupported.desktop</Add-Modules>
        <Add-Exports>jdk.unsupported.desktop/jdk.swing.interop=ALL-UNNAMED</Add-Exports>
    </manifestEntries>
</transformer>

运行要求:运行Jar的环境为完整JDK 11+,不可使用裁剪后的最小JRE

方案2:降级JavaFX版本

JavaFX 15及更早版本的Swing集成组件不依赖jdk.unsupported.desktop模块,仅需将pom.xml中的javafx.version属性值修改为15.0.1,重新打包即可正常运行,无需调整其他配置。

方案3:打包自定义运行时分发包

如果需要分发到无JDK环境,可通过javafx-maven-plugin的jlink能力打包包含所有必要模块的运行包:
修改javafx-maven-plugin配置如下:

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>${javafx.maven.plugin.version}</version>
    <configuration>
        <mainClass>org.openjfx.hellofx.AppRun</mainClass>
        <launcher>hellofx</launcher>
        <jlinkImageName>hellofx-app</jlinkImageName>
        <jlinkZipName>hellofx-app-pack</jlinkZipName>
        <addModules>jdk.unsupported.desktop,javafx.controls,javafx.fxml,javafx.swing</addModules>
    </configuration>
</plugin>

执行mvn javafx:jlink命令后,会在target目录生成可直接分发的压缩包,用户解压后运行对应启动脚本即可,无需本地安装JDK。

额外优化建议

当前pom中javafx-graphics仅配置了win分类器,仅支持Windows系统运行,如需跨平台兼容可删除分类器配置,或者补充linux、mac平台对应的javafx-graphics依赖项。

内容的提问来源于stack exchange,提问作者Woo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:54:02