Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run Scala application in IntelliJ

I'm trying to run a simple Scala snippet,

package example

class HelloWorld extends App {
  println("Hello world")
}

in the IntelliJ IDE with Scala installed. However, the "Run" button appears to be grayed out, and I also don't see it in the context menu (not shown in the screen grab below).

enter image description here

In accordance with the answer of Unable to run Java code with Intellij IDEA, the code is in the src folder which is marked blue. (I've also tried marking it as a 'tests' folder but to no avail). What am I missing?

like image 914
Kurt Peek Avatar asked Oct 12 '17 20:10

Kurt Peek


People also ask

How do I get Scala class in IntelliJ?

On the Project pane on the left, right-click src and select New => Scala class. If you don't see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually.


1 Answers

HelloWorld should be object, not class:

package example

object HelloWorld extends App {
  println("Hello world")
}

For more info about singleton objects, you can see this chapter of "Programming in Scala" book and this question.

like image 125
TeWu Avatar answered Nov 06 '22 15:11

TeWu