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

JavaFX项目Beryx Runtime插件jre任务失败:已声明模块仍找不到javafx.base

解决JavaFX非模块化项目Beryx Runtime插件jlink找不到javafx.base模块问题

问题背景

时隔多年更新JavaFX应用,通过Eclipse启动配置可正常运行,但使用Gradle结合org.beryx.runtime插件执行runtime、jpackage、jpackageImage等依赖jre任务的操作时均失败,生成dmg包受阻。项目为大型非模块化JavaFX项目,依赖org.beryx.runtime插件打包。

环境信息

  • Gradle版本:8.1.1
  • JDK版本:jdk-19.0.2.jdk

build.gradle相关配置

plugins {
    id 'java-library'
    id 'eclipse'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.14'
    id "org.kordamp.gradle.jdeps" version "0.12.0"
    id 'org.beryx.runtime' version '1.12.5'
}
...
ext {
    javaFxVersion = '17.0.7'

    jfxModules = [
        'javafx.base',
        'javafx.controls',
        'javafx.fxml',
        'javafx.media',
        'javafx.web',
        'javafx.swing'
    ]
    ...
}
...
dependencies {
    implementation ('org.openjfx:javafx-controls:$javaFxVersion')
    implementation ('org.openjfx:javafx-graphics:$javaFxVersion')
    implementation ('org.openjfx:javafx-fxml:$javaFxVersion')
    implementation ('org.openjfx:javafx-base:$javaFxVersion')
    implementation ('org.openjfx:javafx-web:$javaFxVersion')

    implementation ("org.controlsfx:controlsfx:$controlsfxVersion") {
        exclude group: 'org.openjfx'
    }
    ... // many others not related to JavaFX
}
...
javafx {
    version = javaFxVersion
    modules = jfxModules
}
...
runtime {

    options = [ '--verbose', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages' ]

    modules = jfxModules

    jpackage {
        ...
    }
}
...

执行任务时的错误信息

taskData: org.beryx.runtime.data.JreTaskData(jreDir:/Users/pf/workspace/myproject/build/jre, options:[--verbose, --strip-debug, --compress, 2, --no-header-files, --no-man-pages], additive:false, modules:[javafx.base, javafx.controls, javafx.fxml, javafx.media, javafx.web, javafx.swing], javaHome:/Users/pf/jdk-19.0.2.jdk/Contents/Home, targetPlatforms:[:])

Executing: [/Users/pf/jdk-19.0.2.jdk/Contents/Home/bin/jlink, -v, --verbose, --strip-debug, --compress, 2, --no-header-files, --no-man-pages, --module-path, /Users/pf/jdk-19.0.2.jdk/Contents/Home/jmods/, --add-modules, javafx.base,javafx.controls,javafx.fxml,javafx.media,javafx.web,javafx.swing, --output, /Users/pf/workspace/myproject/build/jre]

Starting process 'command '/Users/pf/jdk-19.0.2.jdk/Contents/Home/bin/jlink''. Working directory: /Users/pf/workspace/myproject Command: /Users/pf/jdk-19.0.2.jdk/Contents/Home/bin/jlink -v --verbose --strip-debug --compress 2 --no-header-files --no-man-pages --module-path /Users/pf/jdk-19.0.2.jdk/Contents/Home/jmods/ --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.media,javafx.web,javafx.swing --output /Users/pf/workspace/myproject/build/jre

Successfully started process 'command '/Users/pf/jdk-19.0.2.jdk/Contents/Home/bin/jlink''

Error: Module javafx.base not found
java.lang.module.FindException: Module javafx.base not found
    at java.base/java.lang.module.Resolver.findFail(Resolver.java:892)
    . . .

解决方案

问题根源

jlink命令的--module-path仅指向了JDK自带的jmods目录,但JavaFX 17的模块并不包含在JDK 19的jmods中(从JDK 11开始,Oracle JDK和OpenJDK均不再捆绑JavaFX),因此无法找到javafx.base模块。

修复步骤

  1. 配置runtime插件的modulePath
    修改build.gradle中的runtime配置,将JavaFX的jmods路径添加到modulePath中。openjfx插件会自动下载对应平台的JavaFX jmods,可通过javafx.jmodsPath获取该路径:

    runtime {
        options = [ '--verbose', '--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages' ]
        // 可让插件自动检测所需模块,或保留modules配置但确保module-path正确
        // modules = jfxModules
        modulePath = sourceSets.main.runtimeClasspath.files + files(javafx.jmodsPath)
        jpackage {
            // 你的jpackage配置
        }
    }
    
  2. 确保JavaFX平台匹配
    若构建平台为M系列芯片的Mac,需指定对应平台的JavaFX模块,避免下载错误版本:

    javafx {
        version = javaFxVersion
        modules = jfxModules
        platform = 'mac-aarch64' // x64平台用'mac'
    }
    
  3. 升级插件版本
    将org.beryx.runtime插件升级到最新版本(如1.16.0),提升对Gradle 8.x和JavaFX的兼容性:

    id 'org.beryx.runtime' version '1.16.0'
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 10:45:11