Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a Scala script within IntelliJ IDEA?

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:

enter image description here

enter image description here

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.

like image 318
WestCoastProjects Avatar asked Nov 20 '13 04:11

WestCoastProjects


People also ask

How do I run a Scala command line?

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.

How do I run an object in Scala?

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.

How do I run a program locally in IntelliJ?

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.


3 Answers

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.

like image 84
WestCoastProjects Avatar answered Sep 21 '22 20:09

WestCoastProjects


What works for me, is:

  1. Write a Scala script, e.g. MyScript.scala
  2. In the menu select: Run -> Edit Configurations...
  3. Press the "+" (⌘N also works on the Mac in this dialog)
  4. Select "Scala Script"
  5. Then select your Script file in this dialog enter image description here

Now you can run your script. It's a bit inconvenient, but it works.

Note: This works for me in IntelliJ IDEA 14.x

like image 34
Kai Mechel Avatar answered Sep 19 '22 20:09

Kai Mechel


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:

  1. By creating object which extends App

    object test {
        def hi() { print("hi there from here") }
    }
    
    object runnable extends App {
        test.hi()
    }
    
  2. 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()
        }
    }
    
  3. 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"

like image 23
guzial Avatar answered Sep 22 '22 20:09

guzial