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

JavaFX Gradle项目运行报错:进程以非零退出值1结束

问题描述

我用IntelliJ创建了一个新的JavaFX项目,语言选Java,构建系统用Gradle,JDK是Oracle官网下载的21.0.7版本,没加额外依赖。项目创建后运行时报错:

Execution failed for task ':com.example.demogradle.HelloApplication.main()'.

Process 'command 'C:\Program Files\Java\jdk-21\bin\java.exe'' finished with non-zero exit value 1

我试过各种排查方法,检查过JavaFX安装情况,但都没用。用Maven创建相同项目就能正常运行。请问要调整哪些配置才能让这个项目正常运行?

以下是项目的build.gradle配置:

plugins {
    id 'java'
    id 'application'
    id 'org.javamodularity.moduleplugin' version '1.8.12'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.beryx.jlink' version '2.25.0'
}

group 'com.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}
	ext {
    junitVersion = '5.10.2'
}

sourceCompatibility = '21'
targetCompatibility = '21'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'com.example.todoappgradle'
    mainClass = 'com.example.todoappgradle.Launcher'
}

javafx {
    version = '21'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}
解决方案

从配置和报错信息来看,调整以下几点就能解决问题:

1. 修正主类与模块名的匹配问题

报错里的主类是com.example.demogradle.HelloApplication,但你application块里配置的是com.example.todoappgradle.Launcher,两者完全不对应——Gradle找不到指定的主类,直接导致执行失败。

你需要二选一调整:

  • 如果实际主类是HelloApplication,修改application块配置:
    application {
        mainModule = 'com.example.demogradle' // 要和module-info.java里的模块名一致
        mainClass = 'com.example.demogradle.HelloApplication'
    }
    
  • 如果项目结构确实是todoappgradle,则要确保运行的任务对应这个模块,同时检查module-info.java的模块名是否和mainModule一致。

2. 更新JavaFX插件版本

你用的org.openjfx.javafxplugin版本是0.0.13,属于旧版本,对JDK21的兼容性不足。建议更新到最新稳定版:

id 'org.openjfx.javafxplugin' version '0.0.15'

3. 检查module-info.java配置

确保模块化配置正确,module-info.java需要声明JavaFX依赖:

module com.example.demogradle {
    requires javafx.controls;
    requires javafx.fxml;

    opens com.example.demogradle to javafx.fxml;
    exports com.example.demogradle;
}

如果是非模块化项目(没有module-info.java),直接移除org.javamodularity.moduleplugin插件,同时删掉application块里的mainModule,只保留mainClass即可。

4. 统一JDK配置

确保IntelliJ项目SDK和Gradle使用的JDK都是Oracle JDK21.0.7,避免版本不一致导致问题。可以在gradle.properties里添加:

org.gradle.java.home=C:\\Program Files\\Java\\jdk-21

调整完以上配置后,重新同步Gradle并运行项目即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 08:41:01