views:

846

answers:

4

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.

A: 

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])")
}
Bart Kiers
extend Application and using the primary constructor is not recommend for anything else than short "test programs"
Schildmeijer
+1  A: 

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.

Walter Chang
+4  A: 

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

Rich Seller
+1  A: 

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.

Mitch Blevins
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.
Elazar Leibovich