运行菜单项在IntelliJ中消失

时间:2015-07-30 18:19:01

标签: scala intellij-idea

我在IntelliJ中有scala代码,如下所示:

HelloWorld.scala

object HelloWorld {
  //var matchExample = new MatchExample()

  def main(args: Array[String]) = {
    printHello()
    //matchExample.oddEven(1)
  }

  def printHello() = {
    println("hello")
  }

}

MatchExample.scala

class MatchExample {
  def oddEven(x: Int): String = {
    "Hello"
  }
}

如果我取消注释这两行并尝试通过右键单击对象来运行我没有运行菜单项,但是如果我注释掉这两行,那么我确实有“运行”菜单项。 / p>

我缺少什么?

1 个答案:

答案 0 :(得分:2)

原因是您的主方法签名(取消注释matchExample.oddEven(1))与Scala编译器对可运行程序的要求不兼容。

你的是(args:Array [String])字符串,可运行程序的主方法签名是(args:Array [String])单位。如果在Terminal中运行代码,编译器将发出警告。

allen:Desktop allen$ scalac HelloWorld.scala 
HelloWorld.scala:1: warning: HelloWorld has a main method with    
parameter type Array[String], but HelloWorld will not be a runnable program.
Reason: main method must have exact signature (Array[String])Unit
object HelloWorld  {
   ^
one warning found

在Scala中,如果你想编写一个可运行的程序,你最好使用App trait。 这篇文章详细解释了Scala App val initialization in main method

相关问题