简体   繁体   English

kotlin - ClassNotFoundException MainKt

[英]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.

相关问题 IntelliJ Kotlin - MainKt 运行/调试配置不可用,无法运行项目 - IntelliJ Kotlin - MainKt run/debug configuration unavailable, cannot run project IntelliJ 2021.2.3 中的 Kotlin 控制台应用程序 -&gt; 无法在运行/调试配置中添加主 class“MainKt” - Kotlin Console Application in IntelliJ 2021.2.3 -> Cannot add main class "MainKt" in Run/Debug configuration Kotlin 多平台 - “错误:无法找到或加载主类 MainKt” - Kotlin Multi-Platform - “Error: Could not find or load main class MainKt” kotlin 命令返回 ClassNotFoundException - kotlin command returns ClassNotFoundException 具有产品风味和Kotlin的ClassNotFoundException - ClassNotFoundException with product flavors and Kotlin 使用Kotlin创建AAR:ClassNotFoundException - Creating AAR with Kotlin: ClassNotFoundException SpringBoot和Kotlin可执行jar的ClassNotFoundException - SpringBoot & Kotlin executable jar ClassNotFoundException Kotlin parcelable class 抛出 ClassNotFoundException - Kotlin parcelable class throwing ClassNotFoundException 使用 kotlin 自定义风格的 ClassNotFoundException - ClassNotFoundException in custom flavor using kotlin Kotlin集合在Android上抛出ClassNotFoundException - Kotlin collections throw ClassNotFoundException on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM