Sony Arouje

a programmer's log

Archive for March 2011

Running Scala in IntelliJ IDEA 10

with 16 comments

Last couple of days I was playing with Intellij to run Scala. As I am from .NET world Intellij IDEA was a new piece of tool for me. After installing Intellij, I was stumbled what to do next. I started googling and I came to know that Intellij, Netbeans, etc are using plugin models, that means to run Scala I should install plugin for Scala. Great, now how will I instruct Intellij to install Scala plugin for me. It’s very easy to find out, just open Intellij IDEA and you will see the screen as shown below.

Quick note: Intellij Community edition is free and you can download if from Jetbrains site.

image

Have a look at the right top portion of IDEA, you can see a Open Plugin Manager link button. Click the button and will open a window as shown below.

image

Go to the Available tab. The plugin manager will search and show all the available plugin for us. You can iterate through each plugin one by one or Type scala at the search box on the top. Once you find scala in the plugin list. Select it and right click and Say download and install it. That’s it, now we are ready to run scala.

Note: I assume you have already downloaded the Scala complier and new Java JDK and set environment variables properly. if not I will explain later in the post.

Creating our First Scala project

Let us start with famous HelloWorld project Smile. Click File –> New Project. Intellij will show below dialog box.

image

You can see the project name is HelloWorld, also I given the Project file location.

Click Next to continue. If you run for the first time, the IDEA will ask to point to the JAVA JDK installed folder. Just point the installed folder to him. In this scenario I already did that. So now I get a screen as follows

image

Just give the source folder name if you want it. Here I leave it to the default src. Click next button to select the desired technologies we are going to use. In our scenario its Scala.

image

I selected Scala from the list. We are done with creating the project. Just click Finish and complete our wizard.

Write Our First Scala Code

Right click the src folder and select New Package as shown below. I typed MyFirst here. Package is same as the namespace in our .Net world.

image

 

Now right click on the Package MyFirst and say New –> Scala File. I select object from Kind dropdown, I hope you all know what Object in scala means. It will show a dialog as shown below

image

 

I clicked Ok and IDEA shows the code editor, let’s type our first code as shown below

package MyFirst

object FirstApp{
  def main (args:Array[String]){
    println ("hello world")
  }
}

So we done our first Hello World sample.

How to Run our Scala code

Every time I click the run button in IDEA, it will show up the Scala console. And I should run my scala code just like we run it from command prompt. What I wanted was, when I click run, the main function should execute without my intervention. Just like I do in Visual Studio. After some search and playing around in Intellij IDEA, I figured out the way to do it.

Click the small arrow next to the Run button as shown below and click on Edit Configuration.

image

Edit configuration will popup a dialog as shown below.

image

Click the + button in tool bar and select Application from the list. Enter the Fully qualified name in Main Class text box as shown below

image

We are done, now you can see the FirstApp in the dropdown just before the Run button in the Intellij tool bar. Click the Run button or Shift + F10 to execute the code. Below is the screen shot after running the code.

image

 

Configuring Scala Compiler and JDK

Download the latest Scala compiler from Scala Site. Also download the latest JDK.

I copied the Scala compiler in C drive. Next step is add the compiler folder to Path environment variable. Also add the JDK (till bin) to the Path variable. Install JDK if not exist in you machine.

I created a SCALA_HOME and JAVA_HOME environment variable

SCALA_HOME=C:\scala-2.8.1.final

JAVA_HOME=C:\Program Files\Java\jdk1.6.0_21

Append the Path variable with the newly created env variable as shown below.

PATH=$SCALA_HOME%\bin;%JAVA_HOME$\bin; + Path

 

Summary

You can also run Scala in NetBeans and Eclipse as well. You can see more details from Scala site. Initially I configured NetBeans for learning Scala.

Happy coding in Scalaaaaaaaaaaaaaaa.

Written by Sony Arouje

March 18, 2011 at 9:02 am

Posted in Misc

Tagged with , , ,

<span>%d</span> bloggers like this: