【问题标题】:Starter script for Gradle Kotlin application cannot find main classGradle Kotlin 应用程序的启动脚本找不到主类
【发布时间】:2019-06-27 13:50:46
【问题描述】:

我正在尝试使用 Gradle 5.4.1 构建 Kotlin 应用程序。 gradle init生成的代码可以通过./gradlew run运行,我假设我应该可以执行build/scripts生成的脚本。

该脚本由于某种原因找不到主类:(

这是 Gradle、应用程序插件还是我的假设的问题?

$ mkdir example
$ cd example
$ gradle -v

------------------------------------------------------------
Gradle 5.4.1
------------------------------------------------------------

Build time:   2019-04-26 08:14:42 UTC
Revision:     261d171646b36a6a28d5a19a69676cd098a4c19d

Kotlin:       1.3.21
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_151 (Oracle Corporation 25.151-b12)
OS:           Mac OS X 10.14.5 x86_64

$ gradle init

Select type of project to generate:
  ...
  8: kotlin-application
  9: kotlin-library
Enter selection (default: basic) [1..10] 8

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: kotlin) [1..2] 2

Project name (default: example):
Source package (default: example):

$ ./gradlew build
BUILD SUCCESSFUL in 1s

$ ./gradlew run
> Task :run
Hello world.

BUILD SUCCESSFUL in 0s

$ ./build/scripts/example
Error: Could not find or load main class example.AppKt

$ jar tf build/libs/example.jar
META-INF/
META-INF/MANIFEST.MF
example/
example/AppKt.class
example/App.class
META-INF/example.kotlin_module

$ cat build.gradle.kts
plugins {
    id("org.jetbrains.kotlin.jvm").version("1.3.21")
    application
}
repositories {
    jcenter()
}
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}
application {
    mainClassName = "example.AppKt"
}

$ cat src/main/kotlin/example/App.kt
package example
class App {
    val greeting: String
        get() {
            return "Hello world."
        }
}
fun main(args: Array<String>) {
    println(App().greeting)
}

【问题讨论】:

  • ./build/scripts/example 在做什么?我怀疑它应该在寻找 main class example.App 而不是 example.AppKt

标签: java gradle kotlin gradle-kotlin-dsl


【解决方案1】:

它们并不意味着直接运行(参见例如this question)。它们意味着从application plugin 生成的分发包中运行。

你可以通过解压包看到它们是正确的:

$ tar xvf build/distributions/example.tar
$ example/bin/demo
Hello World.

【讨论】:

    猜你喜欢
    • 2018-07-17
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2016-05-29
    • 2021-10-18
    • 2021-07-17
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多