Running Scala in IntelliJ IDEA 10
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.
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.
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 . Click File –> New Project. Intellij will show below dialog box.
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
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.
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.
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
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.
Edit configuration will popup a dialog as shown below.
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
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.
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.
Thanks. Exactly what I was looking for.
Manu
March 26, 2011 at 11:49 pm
You always welcome. Good to know that my post helped u.
Sony Arouje
March 27, 2011 at 9:24 pm
Helped me to get started with Idea. I’m an Eclipse guy. Thanks
Nishant
May 6, 2011 at 11:35 am
Could you pls elaborate what help needs in terms of setting up Intellij IDEA.
Sony Arouje
May 9, 2011 at 12:22 pm
ooops, you got me wrong. I was saying that this post was very helpful to me to get started with IDEA. It was hard to play with menu (because I am too used to of Eclipse) until I landed on your blog. You solved my initial problem, and hence a big THANK YOU. 🙂
Nishant
May 9, 2011 at 3:10 pm
My bad Nishant, sorry I misunderstood your comment. Nice to hear that my post helped you.
Sony Arouje
May 9, 2011 at 3:35 pm
Thank You Very Much.. 🙂
Eshan Sajjad
May 12, 2011 at 11:39 am
Thanks Eshan for your comment.
Sony Arouje
May 12, 2011 at 3:32 pm
Awesome tutorial buddy, thanks a lot you saved me a lota time.
Anonymous
August 19, 2011 at 10:16 am
Thanks dude…After searching long time you have the answer what I want as a beginner.. Thanks a lot again.
Arosha
December 30, 2011 at 2:07 am
Like you I’m a .NET dev just starting out with Scala, and this article was really helpful in getting me through the more painful than necessary first few steps. Much thanks.
Anonymous
September 19, 2012 at 3:32 am
Thanks for leaving your comments and really happy that my post helped you to get started with Scala.
Sony Arouje
September 19, 2012 at 10:50 am
Great article guys. This rellay helped to crystalise my approach for the impending barfoot (Sitecore) site redesign. We deal in large amounts of heavy media all the time, listing photos, video, floorplans etc so this has been on my todo list for some time. Thanks for sharing your approach.
Carlito
November 17, 2012 at 5:10 pm
In Intelli J IDEA You can just Right Click on the Class or Object that can be ran, such as a class with main method or Scala object with main or a jUnit test or a JSP file etc, and hit RUN. It will create a launcher for you 🙂
Anonymous
February 25, 2013 at 12:45 am
Thanks Alexander for sharing it. That was quick and easy and I will keep a note of it.
Sony Arouje
February 25, 2013 at 11:43 am
[…] using this tutorial ‘https://sonyarouje.com/2011/03/18/running-scala-in-intellij-idea-10/’ for creating the first scala HelloWorld with […]
Installing scala in OS X Mountain Lion | Arif Octovian Weblog
July 14, 2013 at 8:45 pm