Black Rock Blog

Zero to Hello World in Scala | BKS2

Zero to Hello World in Scala

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

  1. If you don’t already have it on your system, download Java’s JDK 6 or 7 and install it.
  2. Download Scala 2.10 and install it in /usr/local (eg: /usr/local/scala-2.10.0)
  3. 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.

  1. Launch Intellij (aka the Intellij IDEA 12 CE app) which should bring up the “Intellij IDEA” dialog
  2. Select Configure and then Plugins
  3. Click on the Browse Repositories button and search for “Scala”
  4. 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

  1. In the “Intellij IDEA” dialog select Create New Project
  2. When the “New Project” dialog appears, select Scala Module on the left side
  3. Enter a name & location for your project
  4. If the Project SDK dropdown is set to ““ click New.. to select your JDK installation directory.
  5. 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.
  6. Select the Make global libraries feature to ensure future Scala projects can use this configuration
  7. Select Finish to create your new Scala project

Part 3 - Hello, Scala

  1. 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!
  2. Open your project and right-click on the src folder, then select New -> Scala Class in the contextual menu.
  3. 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.

  1. Extend Scala’s App trait to make your object an entry point for an application. You can do this by changing object Hello to object Hello extends App .
  2. 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:

object Hello extends App {
println("Hello, World")

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:

Hello, World

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.