【问题标题】:Error when running gradle generated script running Kotlin Spring-Boot run运行 Kotlin Spring-Boot run 的 gradle 生成脚本时出错
【发布时间】:2020-12-21 23:14:57
【问题描述】:

我有一个新生成的 spring-boot 项目,其中 gradle 作为构建工具,Kotlin 作为语言。 我在 build.gradle.kts 中放入了一个应用程序插件,并配置了它设置主类。 然后我运行:

gradle build installDist

因此,我得到了 build/install/demo/bin 和 build/install/demo/lib 文件夹。 bin 文件夹包含一个运行我的 spring boot 应用程序的脚本。 不幸的是,当我运行它时出现错误:

Error: Could not find or load main class com.example.demo.DemoApplicationKt
Caused by: java.lang.ClassNotFoundException: com.example.demo.DemoApplicationKt

我的 build.gradle.kts 看起来像这样:

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

plugins {
    application
    id("org.springframework.boot") version "2.3.3.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("jvm") version "1.3.72"
    kotlin("plugin.spring") version "1.3.72"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

application {
    mainClass.set("com.example.demo.DemoApplicationKt")
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "11"
    }
}

我确保类名和包名是正确的,但在运行时出现此错误:./build/install/demo/bin/demo

【问题讨论】:

    标签: spring-boot kotlin gradle


    【解决方案1】:

    我认为您应该删除 application 字段并添加以下 jar 字段:

    jar {
        manifest {
            attributes 'Main-Class': 'com.example.demo.DemoApplicationKt'
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2019-08-23
      • 2018-06-06
      • 1970-01-01
      相关资源
      最近更新 更多