Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - IntelliJ Project Setup

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?

like image 798
Mad Scientist Moses Avatar asked Jun 13 '15 23:06

Mad Scientist Moses


People also ask

How do I create a new project on Kotlin?

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.

How do I run Kotlin main function in IntelliJ?

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.


2 Answers

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!")             }         }     } 
like image 196
Taxist Samael Avatar answered Oct 05 '22 23:10

Taxist Samael


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.

like image 25
2 revs, 2 users 80% Avatar answered Oct 06 '22 00:10

2 revs, 2 users 80%