17

Is there a way to execute a scala script from Eclipse with scala plugin?

A scala script is a scala file which is executed line by line, without forcing you to write a main method. See the Description section in scala's man page.

2
  • The answers are all missing the target. If you are writing a script, to be executed as a script, then there ought to be a way to execute it as such. This is not related to trying things on REPL, or different ways to generate a program instead of a script. Commented Oct 1, 2009 at 13:17
  • 1
    Definitely. None of the answers is related to the question. I wonder why one of them was upvoted. Commented Oct 1, 2009 at 15:35

5 Answers 5

18

I know this question is ancient at this point, but on the off chance that someone is looking for the answer (as I was), here we go. This information is based on the beta 2.0.0.0 release of the Scala Eclipse Plug-in using Scala 2.8.2. I found 2 possible options available:

The first option is to use the "Send Selection to Scala Interpreter" command which is bound to "Ctr+Shift+X" by default. This will take the current text selection in any Scala editor and send it to the interpreter for execution. It creates a new window in the same tab grouping as the Eclipse console. There does appear to be one issue with this in that you get the error ":3: error: value tools is not a member of package scala" at the end of the execution. This is an acknowledged issue and I found workarounds for Inteli-J, but not Eclipse.

The second option (which is the one I'm currently using) is to create a new "Run Configuration" for the Scala interpreter. If go to "Run...Run Configurations" you should see "Scala Interpreter" listed as one of the launchers. You can create a new configuration for this launcher with all default options and when you run it you get the scala interpreter prompt in your console window. From here you can script to your hearts content. You should be able to get it to run script files this way as well, by defining the proper run configurations (though I haven't done that yet).

Hopefully this will be of some use to someone.

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

2 Comments

Thanks for taking the time to write this.
Seems to be working fine now (Eclipse Juno with Scala IDE Helium, Nightly)
10

You can open an interactive shell by right-clicking on the project, selecting Scala-> Create interpreter in XYZ.

Comments

4

If you have the scala distribution installed on the same machine, you can configure the "External Tools" to run scala scripts.

From the menu, select "Run" -> "External Tools" -> "External Tools Configurations...".
Highlight "Program" and create a new configuration.
Fill out the dialog like so:

"Main" tab:
Name: ScalaScriptRunner
Location: c:\path\to\scala\bin\scala.bat [or use the *nix equiv]
Working Directory: ${workspace_loc}
Arguments: ${resource_loc}

"Common" tab:
Check the "Display in Favorites" selection.

Click "Apply" and "Close".

Now, if you highlight a scala script file, you can run it from the toolbar. Note that this is independent of the scala eclipse plugin, and in fact, you may have to name your scripts a different extension (.scalascript?) if you have it installed.

1 Comment

Yeah, I knew you can do that, but I'd rather use the integrated way. If the plugin would differentiate between a script and a program by the shebang (!#) or by allowing you to create a script - that would be great.
2

If I recall correctly, the Scala Eclipse plugin comes with an interactive console in which you can execute your Scala script. Detail of how to activate it has escaped me since I haven't been using Eclipse for Scala development for a long time. Look it up, its there.

Comments

1

Don't know about running a script from Eclipse, but you can do something like this:

object Main extends Application {
  println("Look, no main(args: Array[String])")
}

2 Comments

extend Application and using the primary constructor is not recommend for anything else than short "test programs"
Just for anyone who might read it. Now (since 2.8.something) it's OK to extend Application. The code runs as if it's in the main() function, not in a constructor.

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.