For many developers, the first big hurdle in learning a new language is overcome when they can write and run a basic command-line application in an IDE. Although Scala developers can use the excellent REPL that comes with the scala app, being able to write and run an application in the IDE provides a comfortable foundation for the extensive learning curve of the Scala language (and of functional programming in general).
This tutorial shows how to go from zero to running a basic “Hello, World” Scala application in Intellij IDEA 12 CE IDE (the free, open-source version of Intellij IDEA). The intructions cover OS X but should be applicable with minor effort on Windows and Linux systems.
Part 1 - Setup
- If you don’t already have it on your system, download Java’s JDK 6 or 7 and install it.
- Download Scala 2.10 and install it in /usr/local (eg: /usr/local/scala-2.10.0)
- Download the free Intellij IDEA 12 CE IDE for your system and install it.
The last downloadable, Intellij’s Scala plugin, is installed within the Intellij app.
- Launch Intellij (aka the Intellij IDEA 12 CE app) which should bring up the “Intellij IDEA” dialog
- Select Configure and then Plugins
- Click on the Browse Repositories button and search for “Scala”
- Right-click on the Scala plugin and choose *Download and Install” to install it.
After closing the dialog and restarting Intellij you are ready to create the project.
Part 2 - Create The Scala Project
- In the “Intellij IDEA” dialog select Create New Project
- When the “New Project” dialog appears, select Scala Module on the left side
- Enter a name & location for your project
- If the Project SDK dropdown is set to ”
” click New.. to select your JDK installation directory.
- On the right side of the dialog select Set Scala Home and enter “/usr/local/scala-2.10.0” in the text field immediately below this option. You should see a message that you have selected version 2.10.0, and the Compiler library and Standard library fields should be filled out.
- Select the Make global libraries feature to ensure future Scala projects can use this configuration
- Select Finish to create your new Scala project
Part 3 - Hello, Scala
- When your project appears, click the 1: Project tab on the left side of the window to display the “Project” view. You can also select View -> Tool Windows -> Project from the menu bar. Even better, Intellij ought to have shown this view by default when you create a new project since you can’t get started without it!
- Open your project and right-click on the src folder, then select New -> Scala Class in the contextual menu.
- In the “Create New Scala Class” dialog enter the name “Hello” and set the Kind dropdown to “Object”. Click OK to create the new file.
Now your new Scala object is ready, but it requires some work before you can execute it.
- Extend Scala’s App trait to make your object an entry point for an application. You can do this by changing
object Hello extends App.
- Add the idiomatic Hello, World statement. This simply requires writing
println("Hello, World")in the body of the object.
Your code should look like this:
1 2 3
To run your application, right-click on your Hello object in the Project pane on the left and choose Run ‘Hello.main()’ . After a short period compiling the program you should see the following in the Run view:
I hope you found this short tutorial to be useful. Note that although I have used Intellij IDEA in this article and recommend it for Scala development, many Scala developers prefer the Scala IDE for Eclipse IDE instead.