首页 > 解决方案 > Gradle 多模块 Kotlin 应用程序给出主类未找到错误

问题描述

我创建了一个小项目,其中包含多个模块和很少的代码。我刚刚设置了它,所以只有几乎没有代码的文件夹。结构是这样的:-

demo(root folder)
  -- application(module 1)
    -- src
      -- main
        -- kotlin
          --com.demo
            -- App.kt
    -- build.gradle.kt
  -- login (module 2)
  -- build.gradle.kt
  -- (other build, IDE and gradle folders)

gradle run当我在演示目录中时,我想通过命令(我猜这是使用 gradle 应用程序插件运行项目的默认方式)从 gradle 运行 App.kt 文件(我认为这可能吗?)。我可以轻松地从 IDE 运行 App.kt,也可以gradle run在我在应用程序模块中时运行。下面是根目录下 build.gradle 文件的内容:

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

plugins {
    id("org.springframework.boot") version "2.2.0.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.8.RELEASE" apply false

    kotlin("jvm") version "1.4.30"
    kotlin("plugin.spring") version "1.3.50" apply false
    kotlin("kapt") version "1.4.10"
    
    application
}

repositories {
    jcenter()
}

subprojects {
    repositories {
        mavenCentral()
        jcenter()
    }

    apply {
        plugin("io.spring.dependency-management")
    }
}

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

application {
    // Define the main class for the application.
    mainClassName = "com.demo.AppKt"
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.test {
    useJUnit()
    useJUnitPlatform()
}

但是gradle run从根目录运行会给我这个错误:

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

到目前为止,我已经看到与我的问题相关的各种其他答案,我已经在 build.gradle 中设置了主类,没有错误。我有什么遗漏或我做错了吗?任何帮助将不胜感激。提前致谢!

编辑:包名有错别字

标签: javakotlingradlebuild.gradlegradle-kotlin-dsl

解决方案


您通往主课的路径错误。在您的目录描述中,我看到了路径com.demo.App.kt

尝试从 更改mainClassName = "com.isdb.AppKt"mainClassName = "com.demo.AppKt"


推荐阅读