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?

5
  • 1
    What version of IntelliJ IDEA are you using? Do you have a src folder? Did you create the Kotlin file inside there? Commented Jun 14, 2015 at 6: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. Commented Jun 18, 2015 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. Commented Jun 21, 2015 at 5:26
  • See: stackoverflow.com/a/34513057/3679676 for background on running Kotlin classes, it will help you diagnose your own issues. Commented Jan 5, 2016 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. Commented Jan 5, 2016 at 12:10

10 Answers 10

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!")
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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.

1 Comment

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

Comments

2

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

Comments

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.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.