Setting Up Scala 3 + Java 11 in the IntelliJ IDE

Downloading IntelliJ IDEA

Here's a link to the download page for IntelliJ IDEA. If you scroll down the page you will find a button to download IntelliJ IDEA community version for students and teachers. This is the version you should download.

Java 11.0

Scala runs best on Java 11.0. You should download this from Adopt Open JDK and install it.

Scala 3.0 (for CS 152 only)

This semester we will be using version 3.0 of the Scala programming language. When you install IntelliJ it will suggest loading plugins and you can install the Scala 3 plugin there. Otherwise go to the File/Settings/Plugins menu and download it from there.

Testing

Once installed you should create a Scala project called CS152Labs.

Notice that by default the project uses the sbt build tool.

Notice that I've chosen the latest version of sbt and the latest version of Scala.

Click the Finish button, and be patient. It can take a long time to fetch all of the libraries.

Add a package called greet under your Scala source folder. Add a Scala class to this file called greet.scala. This will generate code for a class called greet, which you can delete. Instead, add the following function definition:

@main def greet(): Unit = println(“Hello World”)

You should be able to run this program from the Run menu or from the file's shortcut menu in the project view window or by clicking the green arrow next to the definition.

Problems?

First make sure you copied the code correctly. greet is an object, not a class (more on this later). And don't forget the "extends App" part and the semi-colon. Also, the println should be indented 2 spaces.

The greet program above uses Scala 3.0 syntax which replaces curly braces by indented code. So, if you see syntax errors it probably means your project is using an older version of Scala. You can verify this by rewriting the code as follows:

object greet extends App {
   prinln("Hello, world")
}

If this is the problem, then look at File/Project Structure/Libraries menu. This should be a path in your file system to the Scala 3.0 library.

Graphical user interface, text

Description automatically generated

 

Notice the standard library path references the scala3 library:

If it's a path to some earlier library, then you will need to remove it, then click the "+" button to add the proper library.

Warnings:

·        This could involve a lot of hunting around on your file system to find it. If you can't find it, you may need to download it from the Scala home page.

·        It's confusing because the platform uses version 2.13.8 of the Scala SDK.

Also, under File/Project Structure/SDKs menu make sure that the JDK home path is set to the Java 11.0 library that you downloaded:

It could also happen that the platform settings are correct but not the project library settings.

(For some reason IntelliJ insists that Scala 2.13.6.jar also be included. Not sure if this is true, but too scared to test it.)