Error: Could not find or load main class in Jar

Error: Could not find or load main class in Jar

我用 Gradle 构建了一个 FatJar,但是如果我执行它,我会得到 "Error: Could not find or load main class de.fabipfolix.OlixBotMain"

不过应该是有的,包名和main-class是对的。 我重命名(重构)了一次,但即使我改回它也没有用。

Gradle:

plugins {
id 'java'
}

group 'de.fabipfolix'
version '1.0'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    implementation 'net.dv8tion:JDA:4.1.1_109'
    implementation 'com.jagrosh:jda-utilities:3.0.2'
    implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.30.1'
    implementation group: 'com.sedmelluq', name: 'lavaplayer', version: '1.3.34'
}

task createFatJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'de.fabipfolix.OlixBotMain'
    }
        from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

有人知道问题出在哪里吗?

您可以在 Gradle user guide 中查看有关如何创建 "fat" jar 的示例。具体来说,您正在从 compile 配置中收集依赖项,该配置已被弃用并且在您的项目中也未使用(您正在使用较新的 implementation 配置正确执行此操作)。但无论哪种情况,您都应该收集运行时依赖项以及编译后的 类。您还需要依赖配置才能解决它。

这是他们在用户指南中的做法:

task uberJar(type: Jar) {
    archiveClassifier = 'uber'

    from sourceSets.main.output

    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
    }
}

或者,您可以使用 Shadow plugin 为您完成这一切。