示例代码可以在https://github.com/KyleMcB/demo找到 我不知道如何在命令行上运行 kotlin jvm 应用程序。 Intelli J 将在其内置的 shell 中以交互方式运行它,但我想将其作为命令行程序分发。所以程序只是尝试使用 readln()
package com.xingpeds
fun main() {
val name = readln()
println("Hello, " + name + "!")
}
和构建文件
plugins {
kotlin("jvm") version "2.0.20"
application
}
application {
mainClass.set("MainKt")
}
group = "com.xingpeds"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(19)
}
当我使用 ./gradlew run 时我得到
euclid@kyles-MBP-3 ~/D/democli (main)> ./gradlew clean run
> Task :run FAILED
Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Users/euclid/Library/Java/JavaVirtualMachines/corretto-19.0.2/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 545ms
3 actionable tasks: 3 executed
当我尝试运行构建文件夹中的脚本时,我得到了
euclid@kyles-MBP-3 ~/D/democli (main)> build/scripts/democli
Error: Could not find or load main class MainKt
Caused by: java.lang.ClassNotFoundException: MainKt
如何运行 jvm 命令行应用程序?
问题就在这里:
mainClass.set("MainKt")
您应该在这里写下主类的完全限定名称,但您只写了它的简单名称
MainKt
。您的主类位于包 com.xingpeds
中,因此其完全限定名称为 com.xingpeds.MainKt
。
mainClass.set("com.xingpeds.MainKt")