Using the IntelliJ IDEA

IntelliJ IDEA is an Integrated Development Environment (IDE) from the company JetBrains. IntelliJ was developed for the Java programming language, but it also has excellent support for Scala via an official plugin.

IntelliJ is the programming environment most of you used in Programming/Ohjelmonti 1, and because it supports sbt projects, you may use it also in this course.

On Aalto Linux computers

Note

The A+ plugin used in Programming/Ohjelmonti 1 course is not (yet) supported in this course. You have to download the exercises and upload your solutions to A+ using your browser. Experimental support for the A+ IntelliJ plugin may become available during the course.

IntelliJ is already installed on the Aalto Linux machines in the computer labs. To start it open a program menu or terminal and run idea.


Installing IntelliJ on your own computer

Note

Make sure to update IntelliJ to the latest community edition!

If you were using IntelliJ during Programming/Ohjelmonti 1 on your own computer, you probably have an old version. This may could cause some issues for you, so we recommend updating. The easiest way to do so is to following the installation instructions below – then you will be asked if you want to uninstall the old version (you can also choose to keep it if you like).

First, if you have used IntelliJ in other courses, you may already have it on your computer. In that case, there is no need to install it again. Simply, make sure that you have updated it to the latest version. You can do this by clicking the ‘cogwheel’ icon in the bottom left corner of the IntelliJ IDEA welcome screen (see figure in next section) and pick Check for Updates in the menu that appears.

Fresh install

To install from scratch, go to the IntelliJ IDEA Downloads page and pick the installer suitable for your system and run it. Make sure to pick the free Community edition of IntelliJ IDEA.

Note that, if you use a package manager (for example on a Linux system) it is likely that you can use this to install IntelliJ directly, without the need to download a separate executable.

After the installation has finished, you should be able to run IDEA from the start/applications menu on your system and be greeted with a welcome screen similar to in the figure below.

IntelliJ welcome screen

Welcome screen shown when you start IntelliJ IDEA. The tab menu on the left lets you choose between Projects, Plugins, and more. (The ‘cogwheel’ in the lower left corner, this will open a menu allowing you to check for updates, and make advanced settings.)

Installing the Scala language plugin

In order to use IntelliJ with the Scala programming language, you will need the official Scala Language plugin. To install it, select Plugins on the welcome screen, and make sure you are viewing the Marketplace. If needed, search for “Scala” in the Marketplace, select the one from JetBrains, and choose to Install it.

Plugin marketplace of IntelliJ IDEA and the scala plugin by JetBrains selected.

Installing the Scala plugin. Select the Plugins tab from the left-hand menu, and make sure you are viewing Marketplace at the top. Then search for and install the “Scala” plugin from JetBrains.

Working with the exercises

Download the exercises package from A+ and unpack it to your computer. Then use the Open button in the Projects tab on the Welcome screen and navigate to the exercise directory you just unpacked.

Select the build.sbt file (or you can just choose to open the whole folder, but make sure it is the right one). If you are asked how you wish the open, choose Open as a Project (not as a file).

IntelliJ IDEA project opening view.

Choose Open from the Project tab and navigate to the exercise folder.

When asked if you want to Trust and Open the project, select Trust Project (you may also want to click in the check-box to automatically trust all projects put in the same containing folder, which is handy if you are only planning on storing course project there).

Note

Treat trust seriously. Build scripts, such as those of sbt, are often full-fledged scripting languages in themselves, and they have the power to read and write files on your computer. For example to seamlessly install missing libraries and execute components as needed. This is why many modern development tools, such as VS Code and IntelliJ, asks you if you trust a project before it imports a project. Take this seriously. If you are uncertain if you trust the authors of a software project you have just downloaded, it is often safest to choose to open it in a preview-mode first. For this course, however, we hope that you trust the teaching staff sufficiently to import the exercises.

IntelliJ will now create the build files for the project, and download any missing Java/Scala libraries needed to build it. The first time you open a project this can take a bit of time.

Running tests and code

Source code containing a main function or tests can be run by clicking the ⏵ buttons next to the line number in the source file. You can also right-click on the file name in the Project tab and choose the corresponding Run action from the context menu, or from the Run menu at the top of the window.

Test results and program output are shown in the “⏵ Run” tab at the bottom of the screen.

IntelliJ IDEA test running view. There should be an arrow to run tests on the line the test class starts.

Tests and programs can be run from the source code (for instance) and the output is shown in the “⏵ Run” tab at the bottom of the screen.

Note

Unit tests and A+ grading performs different tasks. Just because your code passes all unit tests does not guarantee a full score by the grader. On the other hand, you may not necessarily need to solve all the tasks in an exercise to submit your solution for grading and get some points. Most exercises award points for every individual task, so you can get points from an exercise even if some unit tests fail. (But in the real world for professional programming, never ship code which fails a unit test.)

Opening a Scala REPL

There are a couple of different ways to get a Scala REPL in IntelliJ:

Alternative 1: Using the IntelliJ Scala REPL

If you only wish to test out some Scala code, you can open up a Scala REPL inside IntelliJ from the menu ToolsScala REPL… (or by pressing Ctrl+Shift+D).

Note that while you can execute Scala code in this REPL, it is not always well formatted, and this REPL is not by default aware of the exercise structure. To use code in the exercises, or any of the other sub-modules in a round, you will need to configure the REPL Run Configuration:

  1. In the REPL tool bar, click the three dots: , then select Modify Run Configuration

  2. In the dialog that appears, under Use classpath and SDK of module: select the exercise or module you would like to be able to import int he REPL. Then click OK or Apply

  3. Restart the REPL – you should now be able to import functions from that module.

Modifying the Scala REPL run configuration and setting the classpath.

Making REPL aware of sub-module code: 1. Modify Run Configuration, 2. Select module, 3. Restart REPL.

Alternative 2: Opening REPL through the sbt shell

The built-in sbt shell can be used to execute code and run tests through commands, rather than through the IDEA user interface. It also allows you to start a Scala REPL called a console aware of the code in an exercise sub-project. You do this by a command on the form ~<module>/console, where <module> is the name of the sub-project. For example, if you wanted to start a REPL to import code from the sequences sub-project in round 1, you would write ~sequences/console, or if you wanted to play with the tinylog examples in round 3, you would use ~tinylog/console instead (followed by the appropriate import in the REPL, e.g. import tinylog.*).

Starting a REPL aware of tinylog through the sbt shell by ``~tinylog/console``.

Starting a Scala Console through the sbt shell: 1. Open the sbt shell tab, 2. execute ~<module>/console where <module> is the name of a sub-project. Fore example tinylog.

Trouble-shooting

scalac: Token not found:

This error message can appear when you run/test/compile a program due to what seems to be an IntelliJ bug. The fix is to stop and then re-start the Scala Compile server before running/testing/compiling the program again. You can do this by clicking on the small watch/meter icon in the bottom right IntelliJ toolbar.

Scala compile server options that open by clicking the watch/meter icon in the bottom right toolbar. The options are "Run", "Stop", "Configure"

To restart the compile server from this menu, first Stop it then Run it again.

Package name ... does not correspond to...

If IntelliJ shows you this kind of warning message and underlines the package name in e.g. package sequences, it is because it is trying to check that the module names in the source code corresponds to the file structure. This is required in Java, but not in Scala, and IntelliJ may have it turned on.

You can still work the exercises despite this warning, but if you want to turn it off you can got to Settings→Editor→Inspections, then search for ‘Wrong’ using the search bar. In the resulting list look for Wrong package statement and un-check that box. Then click ‘OK’ to close the dialog. This will disable that check and warnings.

IntelliJ inspections settings with the option for Wrong package statement under Scala/General set to off.

To get rid of the Package name… warnings, turn off the Wrong package statement inspection.