Gradle 找不到重复模块/无法找到或加载 Java 中的主要 class 错误 9+

Gradle Recurring Module Not Found / Could not find or load main class error in Java 9+

我正在 运行通过 gradle 安装 JavaFX 11 (TornadoFX 2.0.0-RC1) 应用程序,但每次我使用 运行 任务时,我都会得到一个 "Could not find Module" 或 "Could not find or load main class" 错误。

当我在构建之前 运行 清理时,它工作正常,但在后续构建中,我再次收到错误,直到我 运行 另一个清理。在每次构建之前必须 运行 清理是相当耗时的,所以我希望有一个解决方案可以正确地删除错误。

控制台输出:

14:34:30: Executing task 'run'...


> Configure project :STTSim-TornadoFX
Found module name 'STTSim.TornadoFX'

> Task :STTSim-TornadoFX:compileKotlin
> Task :STTSim-TornadoFX:compileJava
> Task :STTSim-TornadoFX:processResources UP-TO-DATE
> Task :STTSim-TornadoFX:classes

> Task :STTSim-TornadoFX:run FAILED
WARNING: module-info.class ignored in patch: D:\OneDrive\Hot Storage\Coding\Java\Personal\STTSim\STTSim-TornadoFX\build\classes\kotlin\main
Error: Could not find or load main class com.genguava.sttsim.app.SimApp in module STTSim.TornadoFX

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':STTSim-TornadoFX:run'.
> Process 'command 'C:\Program Files\Java\jdk-11.0.1\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s
4 actionable tasks: 3 executed, 1 up-to-date
Process 'command 'C:\Program Files\Java\jdk-11.0.1\bin\java.exe'' finished with non-zero exit value 1
14:34:33: Task execution finished 'run'.

申请build.gradle.kts:

val tornadoFXVersion: String by project

plugins {
    application
    id("org.openjfx.javafxplugin") version "0.0.7"
}

repositories {

}

dependencies {
    implementation("no.tornado:tornadofx:$tornadoFXVersion")
    implementation("com.google.code.gson:gson:2.8.5")
    implementation("org.hildan.fxgson:fx-gson:3.1.2")
    implementation("org.jsoup:jsoup:1.11.3")
}

application {
    mainClassName = "STTSim.TornadoFX/com.genguava.sttsim.app.SimApp"
    applicationDefaultJvmArgs = listOf(
            "--add-opens=javafx.controls/javafx.scene.control=tornadofx",
            "--add-opens=javafx.controls/javafx.scene.control.skin=tornadofx",
            "--add-opens=javafx.graphics/javafx.scene=tornadofx"
    )
}

javafx {
    modules = listOf("javafx.controls", "javafx.media", "javafx.web", "javafx.swing", "javafx.fxml")
    version = "11.0.1"
}

/*
tasks.withType(JavaCompile::class) {
    options.compilerArgs.add("--add-modules=java.sql")
}
*/

tasks.jar {
    manifest {
        attributes(mapOf("Class-Path" to configurations.runtimeClasspath.map { it.asPath }, "Main-Class" to application.mainClassName))
    }
    from(configurations.compile.map { entry -> zipTree(entry) }) {
        exclude("META-INF/MANIFEST.MF")
        exclude("META-INF/*.SF")
        exclude("META-INF/*.DSA")
        exclude("META-INF/*.RSA")
    }
}

sourceSets {
    main {
        output.setResourcesDir(File("build/classes/kotlin/main")) // dodgy workaround
    }
}

根项目build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    java
}

buildscript {
    val kotlinVersion: String by project
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
        classpath("org.junit.platform:junit-platform-gradle-plugin:1.1.0")
    }
}

allprojects {
    val junitVersion: String by project

    repositories {
        mavenLocal()
        mavenCentral()
    }

    apply(plugin = "kotlin")
    apply(plugin = "org.junit.platform.gradle.plugin")

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

    tasks.named<KotlinCompile>("compileKotlin") {
        kotlinOptions.jvmTarget = "1.8"
    }
}

似乎没有应用module-info.class,这可能导致没有定义,mainClassName是什么。构建此模块可能是构建项目的先决条件。

mainClassName = "STTSim.TornadoFX/com.genguava.sttsim.app.SimApp" 可能无效。