In this tutorial, we’ll see how to build a minimal Scala project using IntelliJIDE with the Scala plugin. In this guide, IntelliJ will download Scala for you.
Installation
- Make sure you have the Java 8 JDK (also known as 1.8)
- Run
javac -version
on the command line and make sure you seejavac 1.8.___
- If you don’t have version 1.8 or higher, install the JDK
- Run
- Next, download and install IntelliJ Community Edition
- Then, after starting up IntelliJ, you can download and install the Scala plugin by following the instructions onhow to install IntelliJ plugins (search for “Scala” in the plugins menu.)
When we create the project, we’ll install the latest version of Scala.Note: If you want to open an existing Scala project, you can click Openwhen you start IntelliJ.
Location of the IDE files depends on the operating system, product and version. 2020.1 and above versions 2019.3.x and below versions Examples for IntelliJ IDEA 2020.3: Windows: Configuration (i. Starting from IntelliJ IDEA 2016 and the most recent versions of the lightweight IDEs, we are bundling custom JRE (JetBrains Runtime) with Linux distributions, just like we've been doing for Mac. Our custom JRE is based on OpenJDK and includes the most up to date fixes to provide better user experience on Linux (like font rendering improvements.
Creating the Project
- Open up IntelliJ and click File => New => Project
- On the left panel, select Scala. On the right panel, select IDEA.
- Name the project HelloWorld
- Assuming this is your first time creating a Scala project with IntelliJ,you’ll need to install a Scala SDK. To the right of the Scala SDK field,click the Create button.
- Select the highest version number (e.g. 2.13.5) and click Download. This mighttake a few minutes but subsequent projects can use the same SDK.
- Once the SDK is created and you’re back to the “New Project” window click Finish.
Writing code
Idea Community Edition
- On the Project pane on the left, right-click
src
and selectNew => Scala class. If you don’t see Scala class, right-click on HelloWorld and click on Add Framework Support…, select Scala and proceed. If you see Error: library is not specified, you can either click download button, or select the library path manually. If you only see Scala Worksheet try expanding thesrc
folder and itsmain
subfolder, and right-click on thescala
folder. - Name the class
Hello
and change the Kind toobject
. - Change the code in the class to the following:
Running it
- Right click on
Hello
in your code and select Run ‘Hello’. - You’re done!
Intellij Idea Java
Experimenting with Scala
A good way to try out code samples is with Scala Worksheets
- In the project pane on the left, right click
src
and select New => Scala Worksheet. - Name your new Scala worksheet “Mathematician”.
- Enter the following code into the worksheet:
As you change your code, you’ll notice that it gets evaluatedin the right pane. If you do not see a right pane, right click on your Scala worksheet in the Project pane, and click on Evaluate Worksheet.
Next Steps
Now you know how to create a simple Scala project which can be usedfor starting to learn the language. In the next tutorial, we’ll introducean important build tool called sbt which can be used for simple projectsand production apps.
Intellij Idea Themes
Up Next: Building a Scala Project with IntelliJ and sbt