简体   繁体   English

IntelliJ - 无法在混合(Java/Kotlin)模式下运行 Kotlin 类

[英]IntelliJ - cannot run Kotlin Class in mixed (Java/Kotlin) mode

在此处输入图像描述

I am constantly getting the error (bottom right), but I have configured everything to run Kotlin/Java together.我不断收到错误消息(右下角),但我已将所有内容配置为一起运行 Kotlin/Java。 So, where is the missing bit which I cannot see.那么,我看不到的缺失位在哪里。

UPDATE更新

A comment indicated that my pkg name was wrong, but it didn't matter regardless, as the below updated screenshot shows一条评论表明我的 pkg 名称是错误的,但无论如何都没关系,如下面更新的屏幕截图所示

在此处输入图像描述

Regards,问候,

If you want to write the main function without any parameters, it has to be outside of the class like this:如果你想编写不带任何参数的主函数,它必须在类之外,如下所示:

package org.example

fun main() {
    println("Hello World!")
}

Note that in this case, the main function will be inside an artificial implicit class that is called like the containing file suffixed with Kt .请注意,在这种情况下,主函数将位于一个人工隐式类中,该类的调用类似于以Kt为后缀的包含文件。 This means, that in your example where you place the main function inside a file AppKt.kt , the resulting main class is called AppKtKt .这意味着,在您将 main 函数放在文件AppKt.kt中的示例中,生成的主类称为AppKtKt

If you want to have the main function inside a class, you have to specify a parameter for the arguments.如果你想在一个类中拥有主函数,你必须为参数指定一个参数。 Also, it needs to be static, ie it has to be inside an object and must be annotated with @JvmStatic .此外,它必须是静态的,即它必须位于对象内部并且必须使用@JvmStatic进行注释。 This can be done by declaring an object:这可以通过声明一个对象来完成:

object AppKt {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello World!")
        }
}

Or you can put the main function inside the companion object of a class:或者您可以将 main 函数放在类的伴随对象中:

class AppKt {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            println("Hello World!")
        }
    }
}

在 Kotlin 中,您的 main 函数超出了任何类定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ 中混合 Java/Kotlin 多模块项目中未解决的 Kotlin 参考 - Unresolved Kotlin reference in mixed Java/Kotlin multimodule project in IntelliJ Kotlin类文件无法在IntelliJ for JUnit中运行 - Kotlin class file can not run in IntelliJ for JUnit 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 Gradle项目混合Java和Kotlin,Kotlin找不到Java类 - Mixing Java And Kotlin In Gradle Project, Kotlin Cannot Find Java Class IntelliJ Kotlin - MainKt 运行/调试配置不可用,无法运行项目 - IntelliJ Kotlin - MainKt run/debug configuration unavailable, cannot run project 无法在Java和Kotlin混合项目上注入Dagger2 - Cannot inject with Dagger2 on Java and Kotlin mixed project Kotlin / JVM或Kotlin(Java)— IntelliJ IDEA - Kotlin/JVM or Kotlin (Java) — IntelliJ IDEA 无法用类型参数将 Java class 重写为 Kotlin - Cannot rewrite Java class with type parameters to Kotlin 如何在IntelliJ中运行Kotlin Koans? 什么是“主类”? - How to run Kotlin Koans in IntelliJ? What is the “Main class”? 带有Kotlin的IntelliJ IDEA无法访问其他模块所需的类 - IntelliJ IDEA with Kotlin cannot access class required by another module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM