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):
fun main(args: Array<String>){ println("Hello world!") }
public class Main { fun main(args: Array<String>) { println("Hello world!") } }
What am I missing?
Step 1: In Welcome to Android Studio screen, select Start a new Android Studio Project. Step 2: Select Empty Activity and click on the Next button. Step 3: Here we write name of our application and select the language Kotlin for the project. Then, click on the Finish button to launch the project.
When your project is set up correctly, make sure that your main function is called "main" and has a parameter of type Array<String> . Next to it a Kotlin "K" will appear, which you can click on to run your main function.
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!") } } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With