Kotlin - IntelliJ Project Setup

33k views Asked by At

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):

  • I created a new project, selected "Kotlin - JVM" as the project type.
  • I clicked the "Create..." button for the Kotlin Runtime on the second page and selected "Copy to: lib".
  • I click "Finish" and the project created has one module with same name as my project. There is no default source file or any configuration.
  • I create a Kotlin file named "app.kt" (I've tried other names too, like "Main.kt"), and put the following source code in:
fun main(args: Array<String>){
    println("Hello world!")
}
  • I right clicked on the code editor AND the file in the left pane to find the "Run '_DefaultPackage'" option mentioned in the tutorial, but failed to find it in either.
  • I create a new Kotlin configuration, which asks that I put in a "Main class". Seeing this, I change the code to:
public class Main {
    fun main(args: Array<String>) {
        println("Hello world!")
    }
}
  • I edit my configuration and set the main class to "Main", and then run the configuration. It fails with this error: "Error running : Function 'main' not found in class 'Main'.

What am I missing?

11

There are 11 answers

1
tomjen On

Open your file that contains your main function and go to menu->"Edit configurations" then select "+" in the dialog, "Application" as the type set the name to what you want and set the main class by clicking on the button next to the top entry box (the one labeled "main class").

The select "use class path of module" and select your module from the drop down box. Click "apply" and close the dialog. Now you should be able to run with shift+F10, debug with shift+F9 and edit run configurations with shift+alt+F10. You can also run or debug from the two buttons in the top right hand of your main screen.

0
Mad Scientist Moses On

I had to update my Kotlin plugin (which came out very recently) and then the right click options for running started appearing. I couldn't track the issue down to anything else, so I think that's it.

If you're having this problem, right click on the source code file where your main function is and click run, create, or debug.

Note that the main function has to have the name "main" (no caps), and an "Array" argument. That one caught me a few times when I was making new projects trying to get it to work.

0
Jean_K On

Another solution:

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

remember that it goes outside the class declarations, also the following icon should appear:

run icon

on click:

enter image description here

And problem resolve. If you want try this code in your Main.kt:

import javax.swing.JFrame

fun main() {
    println("Hi, kotlin !!!")
    val window = Main()
}

class Main {
    constructor(){
        val frame = JFrame()
        frame.setSize(800, 600)
        frame.setLocationRelativeTo(null)
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        frame.setVisible(true)
    }
}

Regards !

0
Abdifatah Mohamed On

For those using intelliJ-2021 Community, I found the solution here which basically says put your main function OUTSIDE the class you created. Immediately, the run button will appear and the configuration will update itself

0
Akshay Vijay Jain On

I moved my main.kt file inside the src folder of project and problem solved i.e. IntelliJ could find MainKt class

0
maXp On

Specified "Main class:" -> com.mypackage.MainKt

And create Kotlin file "Main" in package "com.mypackage"

package com.mypackage

fun main(args: Array<String>) {
    println("Hello Kotlin!")
}
1
kshitij On

Its working fine...Please check. copy and paste this code and run.

class MyFirstKotlinClass {

companion object {
    @JvmStatic
    fun main(args: Array<String>) {
        println("=========>>>> run successful")
    }
}

}

0
Europa On

You can manually add a run configuration file by creating a directory and a XML file.

.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.

0
user2323471 On

Just simply right click on the class which you want to run and select Run ClassNameKt option, Rest of will be done by IntelliJ IDE.

0
Taxist Samael On

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!")
            }
        }
    }
0
Jayson Minard On

A full answer for how to identify the runnable class for a top-level main() function, or to use a main() method within a class are both documented in this other Stack Overflow answer: How to run Kotlin classes

This covers running on command-line, within Intellij (as your question asks), Gradle, and more.