Spring Boot jar launch error: Exception in thread "main" java.lang.NoClassDefFoundError

Spring Boot jar launch error: Exception in thread "main" java.lang.NoClassDefFoundError

最近我 运行 jar 我的 Spring Boot Application。我收到错误消息:no main manifest attribute, in temperaturetracker-0.0.1-SNAPSHOT-plain.jar。我已经更新了我的 build.gradle,所以它会包含 Main-Class 属性,但是我确实收到了一个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.example.temperaturetracker.TemperatureTrackerApplication.main(TemperatureTrackerApplication.java:15)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more

我的build.gradle:

plugins {
    id 'org.springframework.boot' version '2.5.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.5.30-RC'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.example.temperaturetracker.TemperatureTrackerApplication'
        )
    }
}

dependencies {
    // Security
    implementation 'org.springframework.boot:spring-boot-starter-security:2.5.3'
    implementation 'io.jsonwebtoken:jjwt:0.9.1'

    // springframework
    implementation 'org.springframework.boot:spring-boot-starter-websocket:2.5.3'
    implementation 'org.springframework.boot:spring-boot-configuration-processor:2.5.3'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3'
    implementation 'org.springframework.boot:spring-boot-starter-validation:2.5.3'
    implementation 'org.springframework.boot:spring-boot-configuration-processor:2.5.3'

    // Firebase
    implementation 'com.google.firebase:firebase-admin:8.0.0'

    // Database
    implementation 'org.postgresql:postgresql:42.2.23.jre7'

    // Testing
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.3'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30-RC'
}

test {
    useJUnitPlatform()
}
compileKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "11"
    }
}

主要class:

package com.example.temperaturetracker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TemperatureTrackerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TemperatureTrackerApplication.class, args);
    }

}

Gradle:

distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip

悬停在 Main-Class 属性警告上也是可见的(不确定这是否与此错误有关):

运行 我的应用程序通常通过按绿色箭头或 (Shift + F10) 正常运行。我希望有人能够解决我的问题。

-plain.jar 文件是一个普通的 jar 文件,没有任何 Spring Boot 的定制。这意味着它不包含您应用程序的依赖项,并且无法使用 java -jar.

启动

您需要 运行 temperaturetracker-0.0.1-SNAPSHOT.jar