Bick
Bick

Reputation: 18511

Intellij - Run scala main file

I have installed Scala plugin and sbt executer in IntelliJ.
I have created a new Scala project and it created a build.sbt file.
In the project setting/Libraries I see a reference to the SBT I have on my computer.
I created a new Scala class with the following code:

class RunMe {
 def main(args: Array[String]): Unit = {
    println("Hello from main of class")
  }
}

I can't seem to find a new type of run configuration to create for the scala class.
I don't see the green play button in the left column (IntelliJ Left Gutter) What am I missing?

How can I configure a run configuration in the code?

Upvotes: 0

Views: 1080

Answers (2)

Suma
Suma

Reputation: 34393

Instead of a class with a static method, which is what you do in Java, you should use object in Scala:

object RunMe {
 def main(args: Array[String]): Unit = {
    println("Hello from main of class")
  }
}

You can also mixin a trait called App instead of providing the main method:

object RunMe extends App {
  println("Hello from main of class")
}

In both cases IntelliJ should pick the definition fine and offer you the green arrow to start the app.

Upvotes: 3

davidrpugh
davidrpugh

Reputation: 4553

I usually just mixin the App trait into my Runner object. Something like...

object RunMe extends App {
 println("Hello from main of class")
}

should do the trick. Intellij should now pickup that this object is "runnable" and provide a "play" button as expected.

Upvotes: 2

Related Questions