Kotlin-IntelliJ项目设置 [英] Kotlin - IntelliJ Project Setup

查看:119
本文介绍了Kotlin-IntelliJ项目设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IntelliJ IDE用Kotlin为JVM启动一个新项目,但是我无法获得使其正常工作的配置.我试图按照本教程进行操作,但此后不起作用( "Run'_DefaultPackage'"选项甚至都没有出现),我开始尝试理解应该做的没有成功的事情. 到目前为止发生的事情(重复):

I want to start a new project with Kotlin for the JVM using the IntelliJ IDE, but I can't get a configuration for it to work. I was attempting to follow this tutorial, and after that didn't work (the "Run '_DefaultPackage'" option never even showed up), I started trying to intuit what was supposed to be done without success. What has happened so far (repeatedly):

  • 我创建了一个新项目,选择"Kotlin-JVM"作为项目类型.
  • 我在第二页上单击Kotlin Runtime的创建..."按钮,然后选择复制到:lib".
  • 我单击完成",创建的项目有一个与我的项目同名的模块.没有默认的源文件或任何配置.
  • 我创建了一个名为"app.kt"的Kotlin文件(我也尝试过使用其他名称,例如"Main.kt"),并将以下源代码放入:
fun main(args: Array<String>){
    println("Hello world!")
}

  • 我右键单击代码编辑器和左窗格中的文件,以找到本教程中提到的运行'_DefaultPackage'"选项,但未能在其中一个中找到它.
  • 我创建一个新的Kotlin配置,要求我输入"Main class".看到这一点,我将代码更改为:
  • public class Main {
        fun main(args: Array<String>) {
            println("Hello world!")
        }
    }
    

    • 我编辑配置并将主类设置为"Main",然后运行配置.失败并显示以下错误:错误运行:在类'Main'中找不到函数'main'.
    • 我想念什么?

      推荐答案

      由于main方法不是静态的,因此无法汇编项目.因此,您应该在随播对象中对其进行定义.

      You can't assemble the project cause main method is not static. So you should define it in companion object.

      class HelloKotlin {
      
              companion object {
                  @JvmStatic fun main(args: Array<String>) {
                      println("Kotlin main is running here!")
                  }
              }
          }
      

      这篇关于Kotlin-IntelliJ项目设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆