32

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.

6
  • It should work in Scala worksheet. Could you try to create a new clean Scala and use Scala home configuration? Commented Nov 20, 2013 at 5:36
  • @lhuang. What do you mean by "a new clean Scala" ? Commented Nov 20, 2013 at 7:50
  • I meant you can try to create a new Scala project. Commented Nov 20, 2013 at 8:24
  • 1
    @lhuang ur right. Creating new scala project then running in Worksheet works OK. It is troublesome that it seems necessary usually to "You have to create a new Scala project" for everything scala to work properly. Also it creates 561 BLANK LINES, don't know why. Commented Nov 20, 2013 at 8:40
  • Yes, create a new Scala project shouldn't be the solution. However, mostly, this kind of errors are caused by some invalid configurations. And it would be hard for us to figure out where. Now, you can make it work in a clean project. so you can compare these two projects to find out what's going on with previous project. Commented Nov 20, 2013 at 8:49

4 Answers 4

20

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.

Sign up to request clarification or add additional context in comments.

Comments

19

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

3 Comments

I tried this but got Scala script not found error! Its strange as I can see the file and have selected it using the file selector of Edit Configuration. Any idea what I might be doing wrong?
I found an interesting observation. Could you please check this stackoverflow.com/questions/54270601/…
Sorry for beeing late to the party Manu Chadha, I just read you comment. Glad there is already an answer in your thread.A really good and long one... I love StackOverflow for the users in here :-)
5

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"

5 Comments

-1 The whole point is to be able to run a script in IJ WITHOUT CHANGES TO THE SCRIPT. Your answer does not address the OP.
author used keywords "script", "compilation", so I gave a hint to compile it. If somebody want to execute script as script I do it as described in post (edited - added info about script)
Your answer is quite different from both the words and the intent. The OP asks for being able to run this from a Run Configuration. If you want to select via the mouse it is completely different. The JetBrains people recognized this as a bug and created a JIRA for it - so trying to answer in this manner is inaccurate.
I'm glad to read your guides. Yes, answer is quite different from these keywords, but what does it mean compile script? In my eyes it look for beginners problem (run simple code) and I tried to help and I think it can be helpful for people who starts Scala with plugin.
The issue here is that you have an answer in mind - that you want to use irrespective of the actual question. The proverbial: if you have a hammer then every problem looks like a nail. This question is not about "how do I use scala in intellij". And there is already an accepted and upvoted answer to that specific question.
1

There are 2 things to understand first. Scala works as both interpreter and compiler. You are trying to with interpreter "scala test.scala" without using "scalac test.scala" and interpreter works without main method also.

Other way you can do in intellij is open the terminal or command prompt in intellij and run scala test.scala from the file location (Pre-requisite is scala in present the system path)

1 Comment

Umm intellij is probably the most powerful scala IDE out there. It does support scala scripts (in interpreted mode). Your comment about interpreter vs compiler is something I knew about on my first day of scala in October 2010.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.