In this tutorial, we’ll see how to build a minimal Scala project using IntelliJ IDE 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 on how 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 Open when you start IntelliJ.
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.15) and click Download. This might take 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
- On the Project pane on the left, right-click
src
and select New => 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 file to the following:
object Hello extends App {
println("Hello, World!")
}
@main def hello(): Unit =
println("Hello, World!")
In Scala 3, you can remove the object Hello
and define a top-level method
hello
instead, which you annotate with @main
.
Running it
- Right click on
Hello
in your code and select Run ‘Hello’. - You’re done!
- Right click on
hello
in your code and select Run ‘hello’. - You’re done!
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:
def square(x: Int): Int = x * x
square(2)
As you change your code, you’ll notice that it gets evaluated in 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 used for starting to learn the language. In the next tutorial, we’ll introduce an important build tool called sbt which can be used for simple projects and production apps.