2013-10-24 135 views
2

我刚刚安装了Eclipse Scala IDE并导入了一些现有项目。但是,当我想通过右键单击 - > Run来运行类(包含主def)时,我只会得到'运行配置...'。我如何运行这些Scala类?在Scala IDE中运行类

(我已经检查了“斯卡拉自然”被添加。)

+2

当你说你“想运行类”时,你是在谈论一个“对象”还是“类”。此链接可能有所帮助:http://stackoverflow.com/questions/7430235/how-to-move-main-method-to-another-class-in-scala –

回答

10

你需要运行的对象不是一类由urban_racoons指出。这样你就可以运行:

object MyApp{ 
    def main(args: Array[String]): Unit = { 
    println("Hello World") 
    } 
} 

object MyApp extends App { 
    println("Hello World") 
} 

斯卡拉不能因为主要的方法必须是静态运行的类。这只能由编译器在Scala的单例对象后面创建。只要打开Scala透视图,创建对象并且“作为Scala应用程序运行”应该出现在“run”上下文子菜单中。

您仍然可以访问使用App特质程序的参数作为

object MyApp extends App { 
    println("The programme arguments are:") 
    args.foreach(println) 
    // the above is just shorthand for 
    // args.foreach(ar => println(ar)) 
} 

还应当指出的是,应用程序是一个特点。所以它可以与其他特性和任何类别混合使用,这使得它可以用于快速原型设计。