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