简体   繁体   中英

Starter script for Gradle Kotlin application cannot find main class

I'm trying to build a Kotlin application using Gradle 5.4.1. The code generated by gradle init can be run via ./gradlew run , and I assumed I should be able to execute the script generated in build/scripts .

That script however cannot find the main class for some reason :(

Is this a problem with Gradle, the application plugin, or my assumptions?

$ 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)
}

They are not mean to be run directly (see eg this question ). They are mean to be run from the distribution package generated by the application plugin .

You can see that they are correct by extracting the package:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM