我有简单的脚本
package com.lapots.game.journey.ims.example
fun main(args: Array<String>) {
println("Hello, world!")
}
这是 gradle
任务
task runExample(type: JavaExec) {
main ='com.lapots.game.journey.ims.example.Example'
classpath = sourceSets.main.runtimeClasspath
}
但是当我尝试运行任务时 gradle runExample
我收到错误
Error: Could not find or load main class com.lapots.game.journey.ims.example.Example
运行应用程序的正确方法是什么?
感谢这个链接 如何在Kotlin中运行已编译的类文件? 由@JaysonMinard提供
那 main
@file:JvmName("Example")
package com.lapots.game.journey.ims.example
fun main(args: Array<String>) {
print("executable!")
}
然后 task
task runExample(type: JavaExec) {
main = 'com.lapots.game.journey.ims.example.Example'
classpath = sourceSets.main.runtimeClasspath
}
做了伎俩
如果您使用的是Kotlin构建文件 build.gradle.kts
你需要做的
apply {
plugin("kotlin")
plugin("application")
}
configure<ApplicationPluginConvention> {
mainClassName = "my.cool.App"
}
如果你申请 application
插件使用 plugins {}
阻止你可以使它更简单:
plugins {
application
}
application {
mainClassName = "my.cool.App"
}
详情见 https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md
你也可以使用 gradle
应用程序插件。
// example.kt
package com.lapots.game.journey.ims.example
fun main(args: Array<String>) {
print("executable!")
}
将此添加到您的 build.gradle
// build.gradle
apply plugin "application"
mainClassName = 'com.lapots.game.journey.ims.example.ExampleKt'
然后运行您的应用程序如下。
./gradlew run