How to get started with Scala on Windows – Part 1

scala-logo

Scala is a statically typed, object-oriented programming language that blends imperative and functional programming styles. Today’s post is aimed at teaching you how to get started with Scala in a Windows system.

Install the JDK

  • Download the JDK for Windows from the Oracle website
  • Run the installer
  • Add the bin installer to the PATH environment variable, described here.
  • Check if you have JDK installed by running the following snippet in your command prompt:
    for %i in (java.exe) do @echo.   %~$PATH:i

Install Scala and sbt

  • Go to the official Scala website and download the executable.
  • Download sbt from its official website
  • Check if you have sbt installed properly by running the following snippet in your command prompt:
sbt about

IntelliJ IDEA

Download and install IntelliJ IDEA

Before you create or open a Scala project, you need to install the Scala plugin. For that, use the Configure → Plugins → Browse JetBrains Plugins from the Welcome Screen, or Preferences (Settings) → Plugins and search for Scala.

The Scala plugin requires restart to complete installation.

Setup the JDK

From the welcome screen, go to Configure → Project defaults → Project structure and add the JDK.

Create a project

Go to the project wizard. Click create new project. Select Scala and finally SBT.

Click next to specify your project name and desired location. After you enter the information IntelliJ IDEA will create an empty project containing a build.sbt file.

Creating a Scala Worksheet

Navigate to your project root and open the context menu with your right-click New → Scala Worksheet

To make sure the worksheet is working, insert the basic “Hello World” inside it and theb use the corresponding toolbar icon, or press Alt+Ctrl+W

Then be sure to navigate into the scala worksheet folder.

Creating a Scala class

Much akin to worksheets, Scala classes are created via context menu action Create New → Scala class

Name the class App

Now create a package the method is the same as classes or worksheets.

And then a package-object, with the name example, package objects are created akin to classes and worksheets. Insert the following code in the package-object file:

package example

object ExampleApp extends App
{
  println("Hello World");
}

Now use the context menu to run your application, or use Ctrl + Shift + F10

After the application has finished running, you’ll see its output in the Run tool window.

Go to File → Save to save your project and exit IntelliJ IDEA.

Opening a SBT Project

To open an SBT project in IntelliJ IDEA, go to the Welcome Screen, click Import Project, and select SBT build file that you wish to open. IntelliJ IDEA will then create a new project and import the file to it.

 

Synchronizing SBT and IntelliJ IDEA projects

IntelliJ IDEA SBT support synchronizes the project with your build file, so when you change Scala version you’re going to use, or add a library, your project is updated accordingly. For the next time, you can avoid this step by checking off the option ”Use auto-import” in the previous step.

Warning: the scala version shown in this screenshot is 2.10.3 and it’s outdated. The scala version that we will use for this course is 2.11.x. Also, note that the scalatest dependency has the scala version embedded in “scalatest_2.10” and that there’s no `2.0` version for 2.11.x. So, replace:

libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.0" % "test"
with
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.6" % "test"
 

The double percentage symbol will force sbt to use the current scala version defined in scalaVersion (which has to be “2.11.7” or “2.11.8”).

Using the terminal to run sbt commands

The easiest way to run SBT commands from from IntelliJ IDEA is to use the Terminal tool window via Alt+F12.

To learn more about IntelliJ IDEA, please refer to their official documentation.

This is all for part 1, the part 2 will explain how to start up sbt and running its scala interpreter.