idea scala 跑,如何在IntelliJ IDEA中运行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>

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

expected class or object definition

test.hi()

^

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:

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

Note: the Scala-Library is properly set up:

c40e1bf3a598d3e5b52ff1acfba50bff.png

b52cecde01ca92ebc6deeec6573b1d19.png

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) create a brand new Scala project (as suggested by @lhuang) and

(b) when running a script, you need to go into the Run Configuration and remove the Make step (as mentioned in the 'related' SOF question).

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).

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值