如何在IntelliJ IDEA中运行Scala脚本? [英] How to run a Scala script within IntelliJ IDEA?

查看:2561
本文介绍了如何在IntelliJ IDEA中运行Scala脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的scala脚本:

Here is a trivial scala script:

object test {
  def hi() { print("hi there from here") }
}


test.hi()

从命令行执行预期的操作:

From the command line it does the expected:

scala /shared/scaladem/src/main/scala/test.scala
Loading /shared/scaladem/src/main/scala/test.scala...
defined module test
hi there from here
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

但在Intellij中,它会产生编译错误。右键单击|运行test.scala

But within Intellij it gives a compilation error. Right click | Run test.scala

expected class or object definition
test.hi()
^

BTW我也尝试过运行scala工作表。这更糟糕 - 大量的垃圾输出甚至没有接近编译。

BTW I also tried running as a scala worksheet. That was MUCH worse - tons of garbage output and did not even get close to compiling.

更新:它似乎有一个较旧但相似的问题:

Update: it appears there is an older but similar question:

如何在Intellij Idea 10中运行Scala代码

How to run Scala code in Intellij Idea 10

我进入运行配置并按照指示取消选中生成 (这很麻烦,但也是如此..)

I went into Run Configuration and unchecked "Make" as instructed (this was bothersome but so be it ..)

然而,在做出改变后我得到了一个不同的错误:

However after making that change I get a different error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Either
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: scala.Either
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 3 more

注意:Scala-Library是正确设置:

Note: the Scala-Library is properly set up:

另一个更新(在@ lhuang的评论之后)我按照建议从头开始创建另一个项目。在这种情况下,Scala工作表正常工作(test.sc)。但是scala脚本(通过scala test.scala运行命令行时可以正常工作)仍然可以工作,即使在这个全新的scala项目中也是如此。

Another update (after @lhuang's comment below) I followed the suggestion to create another project from scratch. In that case a Scala Worksheet worked properly (test.sc). But a scala script (which works when running command line via "scala test.scala" ) still does not work, even in this brand new "scala" project.

推荐答案

这里的答案是项目的组合:

The answer here is a combination of items:


  • (a)创建一个全新的Scala项目(由@lhuang建议)和

  • (b)在运行脚本时,您需要进入运行配置并删除Make步骤(如'相关'SOF问题中所述)。

这显示了Intellij及其scala插件的粗糙边缘。特别是当我想要将scala与java集成时,如果甚至可能在此时使用Intellij显然很困难(需要频繁创建新的Scala项目对于大多数试图合并scala的java项目来说都是非首发)。

This shows rough edges with Intellij and its scala plugin. Especially when I want to integrate scala with java it is apparently difficult if even possible using Intellij at this time (need to create new Scala project on a frequent basis is a non-starter for mostly java projects attempting to incorporate scala).

但是对于scala-first项目来说,似乎这可能是可行的。

But for scala-first projects it seems this may be workable.

这篇关于如何在IntelliJ IDEA中运行Scala脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆