2017-10-12 27 views
0

我想運行一個簡單的Scala代碼段運行的Scala程序,無法在的IntelliJ

package example 

class HelloWorld extends App { 
    println("Hello world") 
} 
在IDE的IntelliJ使用Scala

安裝。然而,在「運行」按鈕,似乎是灰色的,我也沒有看到它在上下文菜單(在下面的屏幕抓取未顯示)。

enter image description here

按照Unable to run Java code with Intellij IDEA答案,該代碼是在其中被標記爲藍色src文件夾。 (我也嘗試將其標記爲「測試」文件夾,但無濟於事)。我錯過了什麼?

回答

6

HelloWorldobject,不class

package example 

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

有關單一對象的詳細信息,你可以看到 「編程在斯卡拉」 的書,this questionthis chapter

0

要運行任何scala應用程序,您需要singleton對象,它將擴展應用程序或定義採用一個參數的主方法,Array [String],並且結果類型爲Unit。 與適當的簽名的一個主要方法的任何獨立的對象可以被用作入口點的應用程序。

所以,你可以運行以下兩個方面的應用階。

package example 

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



object HelloWorld { 
     def main(args: Array[String]) { 
      println("Hello world")    
     } 
    }