38

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?

Mad Scientist Moses
  • 1,577
  • 1
  • 12
  • 11
  • 1
    What version of IntelliJ IDEA are you using? Do you have a src folder? Did you create the Kotlin file inside there? – Hadi Hariri Jun 14 '15 at 06:08
  • IntelliJ version 14.1.3. I have a source folder named "src" created by default and it has a Kotlin class file in it named Main. – Mad Scientist Moses Jun 18 '15 at 22:55
  • Can you show a screenshot of your project layout or better yet, send me the sample? I'm curious as to why this is happening. – Hadi Hariri Jun 21 '15 at 05:26
  • See: http://stackoverflow.com/a/34513057/3679676 for background on running Kotlin classes, it will help you diagnose your own issues. – Jayson Minard Jan 05 '16 at 12:09
  • Note in your question the `class Main` is missing the `companion object` within, and the `@JvmStatic` annotation on the `main()` method. See the link I posted in the previous comment for more information. – Jayson Minard Jan 05 '16 at 12:10

11 Answers11

66

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!")
            }
        }
    }
Taxist Samael
  • 1,157
  • 1
  • 9
  • 23
11

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.

Community
  • 1
  • 1
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
8

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!")
}
maXp
  • 1,428
  • 1
  • 15
  • 23
5

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.

tomjen
  • 3,779
  • 3
  • 29
  • 35
  • The triple dot button for selecting the class didn't work (double clicking the class didn't do anything), but I was able to type in the name of my class there. Now it gives me an error "main method should be static", which for Kotlin makes no sense because there is no such thing. I tried to take it out of the class, but that didn't work. – Mad Scientist Moses Jun 18 '15 at 23:13
3

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 !

Jean_K
  • 51
  • 7
2

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

Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
1

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")
    }
}

}

kshitij
  • 135
  • 1
  • 1
  • 8
  • The highest upvoted answer to this question lists virtually the same code as you, has the code properly formatted, and provides an explanation on top of it. What exactly are you trying to contribute here? – Robby Cornelissen Dec 17 '19 at 13:15
0

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.

Mad Scientist Moses
  • 1,577
  • 1
  • 12
  • 11
0

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

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.

Europa
  • 974
  • 12
  • 40
-1

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.