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.
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:
object ExampleApp extends App
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: