简体   繁体   English

IntelliJ 2021.2.3 中的 Kotlin 控制台应用程序 -> 无法在运行/调试配置中添加主 class“MainKt”

[英]Kotlin Console Application in IntelliJ 2021.2.3 -> Cannot add main class "MainKt" in Run/Debug configuration

I have created a new Kotlin Console Application in IntelliJ using Project JDK openjdk.17.我使用 Project JDK openjdk.17 在 IntelliJ 中创建了一个新的 Kotlin 控制台应用程序。

I have changed the gradle version to 7.3 in the file gradle/gradle-wrapper.properties:我已在文件 gradle/gradle-wrapper.properties 中将 gradle 版本更改为 7.3:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

When I create a Kotlin Console Application project in IntelliJ it creates a file named "Main.kt".当我在 IntelliJ 中创建 Kotlin 控制台应用程序项目时,它会创建一个名为“Main.kt”的文件。 The file is not a class, but a function:该文件不是 class,而是 function:

fun main(args: Array<String>) {

}

On one of my computers at home I can run the program by clicking the Run button (SHIFT+F10) in IntelliJ.在我家中的一台计算机上,我可以通过单击 IntelliJ 中的“运行”按钮 (SHIFT+F10) 来运行该程序。 On another computer with the same setup the Run button is grey.在具有相同设置的另一台计算机上,运行按钮为灰色。 Here I need to add a Run/Debug Configuration.这里我需要添加一个运行/调试配置。

In "Run/Debug Configuration" I select + then Kotlin.在“运行/调试配置”中,我是 select + 然后是 Kotlin。 In Main class I cannot select "MainKt".在主要 class 我不能 select “MainKt”。

What I have tried:我试过的:

  • Invalid cache/Restart无效的缓存/重新启动
  • Create new project, but I get the same problem创建新项目,但我遇到了同样的问题

My source code:我的源代码:

src\main\kotlin\Main.kt: src\main\kotlin\Main.kt:

fun main(args: Array<String>) {
    println("Hello World!")

    // Try adding program arguments via Run/Debug configuration.
    // Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
    println("Program arguments: ${args.joinToString()}")
}

build.gradle.kts: build.gradle.kts:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.5.10"
    application
}

group = "me.admin"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnit()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "1.8"
}

application {
    mainClass.set("MainKt")
}

gradle.properties: gradle.properties:

kotlin.code.style=official

settings.gradle.kts:设置.gradle.kts:

rootProject.name = "untitled"

gradle\wrapper\gradle-wrapper.properties: gradle\wrapper\gradle-wrapper.properties:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

After debugging I found out that I could make a run configuration file manually by creating a directory and a XML file.调试后我发现我可以通过创建一个目录和一个 XML 文件来手动制作一个运行配置文件。

.run/MainKt.run: .run/MainKt.run:

<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="MainKt" type="JetRunConfigurationType" nameIsGenerated="true">
    <option name="MAIN_CLASS_NAME" value="MainKt" />
    <module name="untitled.main" />
    <shortenClasspath name="NONE" />
    <method v="2">
      <option name="Make" enabled="true" />
    </method>
  </configuration>
</component>

You can change untitled.main with the name of your project.您可以使用项目名称更改untitled.main

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM