[英]kotlin - ClassNotFoundException MainKt
I have created a new project with New Project -> Kotlin Multiplatform -> Library
.我用
New Project -> Kotlin Multiplatform -> Library
创建了一个新项目。 Then the IDE created few source trees in the root project, which I have removed and added a new module called kmm
.然后 IDE 在根项目中创建了几个源代码树,我已将其删除并添加了一个名为
kmm
的新模块。
Now the problem I have is that there is no Run button next to main fun.现在我遇到的问题是 main fun 旁边没有运行按钮。 If I run
run
task from application
plugin, then I get如果我从
application
插件运行run
任务,那么我得到
> Task :wrapper
BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
> Task :kmm:compileJava NO-SOURCE
> Task :kmm:processResources NO-SOURCE
> Task :kmm:classes UP-TO-DATE
> Task :kmm:run FAILED
1 actionable task: 1 executed
Error: Could not find or load main class com.github.shalva97.MainKt
Caused by: java.lang.ClassNotFoundException: com.github.shalva97.MainKt
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':kmm:run'.
> Process 'command 'C:\Program Files\AdoptOpenJDK\jdk-11.0.11.9-hotspot\bin\java.exe'' 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 256ms
11:02:41 PM: Execution finished 'run'.
So how can I run the code which is inside my new module?那么如何运行新模块中的代码呢?
Here is the root gradle:这是根 gradle:
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
Here is gradle for kmm
module:这是
kmm
模块的 gradle:
plugins {
kotlin("multiplatform") version "1.8.0-Beta"
application
}
repositories {
mavenCentral()
}
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
}
tasks.getByName<Test>("test") {
useJUnitPlatform()
}
tasks.test {
useJUnitPlatform()
}
kotlin {
jvm()
linuxArm64()
sourceSets {
val commonMain by getting
}
}
application {
mainClass.set("com.github.shalva97.MainKt")
}
my Main.kt我的主要.kt
package com.github.shalva97
fun main() {
println(5.factorial())
}
fun Number.factorial(): Long {
return (1..this.toLong()).fold(1) { i: Long, l: Long ->
return l * i
}
}
And here is the pic, maybe it will also help:这是图片,也许它也会有所帮助:
EDIT:编辑:
Here is the settings.gradle.kts:这是 settings.gradle.kts:
rootProject.name = "Willians"
include("kmm")
Here is my workaround I found.这是我找到的解决方法。
add a new native target with binaries
block使用
binaries
块添加新的本机目标
macosX64 {
binaries {
executable {
entryPoint = "com.github.shalva97.main" // here main is the function name
}
}
}
and then in Gradle a new task should appear runDebugExecutableMacosX64
, which will run and show the output.然后在 Gradle 中应该出现一个新任务
runDebugExecutableMacosX64
,它将运行并显示 output。
There is actually 2 of them, not sure why but both work:实际上有 2 个,不知道为什么但都有效:
19:51:29: Executing 'runDebugExecutableMacosX64'...
> Task :wrapper
BUILD SUCCESSFUL in 2s
1 actionable task: 1 executed
> Task :kmm:compileKotlinMacosX64
> Task :kmm:linkDebugExecutableMacosX64
> Task :kmm:runDebugExecutableMacosX64
[1, 2, 3, 5, 7, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
BUILD SUCCESSFUL in 3s
3 actionable tasks: 3 executed
19:51:35: Execution finished 'runDebugExecutableMacosX64'.
Now application
plugin seems useless, so I guess it can be removed.现在
application
plugin好像没用了,估计可以去掉了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.