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