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:
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
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.
To run Scala from the command-line, download the binaries and unpack the archive. Start the Scala interpreter (aka the “REPL”) by launching scala from where it was unarchived. Start the Scala compiler by launching scalac from where it was unarchived.
Step 1: Compile above file using scalac Hello. Scala after compilation it will generate a Geeks. class file and class file name is same as Object name(Here Object name is Geeks). Step 2: Now open the command with object name scala Geeks.
From the main menu, select Run | Edit Configurations. and select the configuration type that corresponds to the application server that you will use. Use a Local application server run configuration if you want it to start the server locally before deploying the artifacts.
The answer here is a combination of items:
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.
What works for me, is:
Now you can run your script. It's a bit inconvenient, but it works.
Note: This works for me in IntelliJ IDEA 14.x
Your code works in command line because it is "script", when you want to make it runnable in project there are ways to do it:
By creating object which extends App
object test {
def hi() { print("hi there from here") }
}
object runnable extends App {
test.hi()
}
or java-like solution i.e. creating main method
object test {
def hi() { print("hi there from here") }
}
object runnable {
def main(args: Array[String]) {
test.hi()
}
}
when need to execute as script - I do it like that: select code to execute by mouse, then choose from context menu "Send Selection To Scala Console"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With