Wednesday, July 30, 2008

A novice Scala programmer: Eclipse vs. IntelliJ IDEA vs. Netbeans. Creating and running Scala project with Eclipse.

In a previous Scala post I described my experience with installation of Eclipse, IntelliJ IDEA, and Netbeans applications and installation of Scala plugins in those environments. Now, let's now follow the process of creating, configuring, running, and debugging a simple Scala project with Eclipse. As a test project I will use "Rational" class defined in chapter three of the wonderful Programming in Scala book written by Martin Odersky, Lex Spoon, and Bill Venners.

III. A process of creation Scala application with Eclipse plugin is roughly described on Scala Plugin for Eclipse page.
1. Start Eclipse. Use File New Scala Project menu and create chp6Rational Scala project in an appropriate folder. Eclipse automatically create a project folder with a name of your project, so you would just need to specify chp6Rational as a project name. When Scala project is created, Eclipse automatically creates "src" sub folder.

2. Expand the "chp6Rational" project tree in "Package Explorer", select "src"folder, right click and choose New Package. Name it "rational".

3. Expand the project tree, select "rational" package, right-click and choose New Scala Class. name it "Rational". Add an appropriate code from "Programming in Scala".
Do it again, this time creating new "MyApp" Scala Object and add def main(args: Array[String]) { } code.

4. Save everything.

IV. Here's how to set, compile, run or debug Scala application with Eclipse.

1. Select "chp6Rational" project in Package Explorer, click Run Run Configurations..., double-click or right-click on Scala Application to create a new one. Name your application, type chp6Rational as Project name, type fully qualified main class name as PackageName.ObjectWithMainMethodName (rational.MyApp in my case).
2. Hit "Run" button to compile and run your Scala application. What really drives me crazy is the fact, you'll need to repeat steps 1-2 (except naming application, project, and main class) every time you want to run your Scala application. "Run" button on Eclipse toolbar doesn't work correctly with Scala.

3. Debugger works nicely with Scala application. You can set breakpoints by double-clicking gray area to the left of your code, execute code line-by-line, see values of variables.
Repeat steps 1-2 above, but instead of Run Run Configurations... start from Run Debug Configurations... After you finish debugging in Eclipse, close Debug Perspective by clicking on "Scala" perspective in the top right corner.