Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Using IntelliJ IDEA

If you wish to use IntelliJ to do the tasks, please follow the advice on this page.

You can stop reading right now if you do not intend to use IntelliJ!

Note that the procedure for setting up an IntelliJ project varies, depending on whether or the task is managed by a build tool or not, and which build tool is used. The subsections below cover all three possibilities.

Warning

Note that you cannot use IntelliJ in a Codespace or on SoCS lab machines!

You should not attempt to install it into those environments. The advice provided here assumes that you have installed it on your own computer.

Working on Basic Tasks

Most of the tasks do not have any particular structure, and they are not managed by a build tool such as Gradle or Amper.

To use IntelliJ for these tasks:

  1. Move to the relevant task directory (e.g., task1_1). Inside that directory, create a subdirectory named src.

    If there are existing .kt files in the task directory, move them into src.

  2. Start IntelliJ. On the Welcome screen, click New Project. Make sure that ‘Kotlin’ is selected as the project type, from the list of options on the left of the dialog.

  3. In the Location text field, specify the path to the tasks directory in your repository, i.e., the parent directory of all the tasks.

  4. In the Name text field, specify the directory name for the task you are working on (e.g., task1_1). Don’t worry about the ‘Directory is not empty’ warning that pops up.

  5. IMPORTANT: do NOT tick the ‘Create Git repository’ checkbox!

  6. Leave Build System set to ‘IntelliJ’.

  7. Make sure that the JDK setting references a valid JDK version on your system.

  8. Leave the ‘Add sample code’ checkbox unticked. Make sure that the ‘Use compact project structure’ box is ticked.

  9. Click the Create button.

When the main window appears, it should show the README file associated with the task.

To add code to the project, right-click on the src folder in the Project panel on the left of the screen and select New > Kotlin Class/File. Choose the appropriate file type from the list of options, enter a name for the file, then press Enter.

To run code, open a source code file that contains a main() function and click on the green triangle in the margin of the editor, at the start of the function definition.

Working on a Gradle Project

If a task directory in your repository contains files named gradlew and build.gradle.kts, it is a Gradle project. The procedure for working with one of these projects is fairly simple.

  1. Start IntelliJ. On the Welcome screen, click the Open button.

  2. Select a directory containing a Gradle project (e.g., task1_4). A dialog will appear, asking whether you trust the project. Click the Trust Project button.

  3. Wait for the project to import. Note: this might take a while!

When the main window appears, it should show the README file associated with the task.

To add code to the project, navigate down to the src/main/kotlin directory in the Project panel on the left of the screen. Right-click on this directory and select New > Kotlin Class/File. Choose the appropriate file type from the list of options, enter a name for the file, then press Enter.

To run unit tests for the first time, click on the Gradle button (the one with the elephant icon) in the toolbar on the right of the window. Expand the list of Gradle tasks that appears in the Gradle tool panel. Look under the heading ‘verification’ for a task named ‘test’ and double-click on it.

To run an application for the first time, look under the heading ‘application’ in the list of Gradle tasks that appears in the Gradle tool panel. Double-click on the task named ‘run’.

IntelliJ will create run configurations for the ‘test’ and ‘run’ tasks when you use them for the first time. These will appear in a drop-down menu at the top of the window. You can use this menu and the Run button to run the tests or the application in future.

Working on an Amper Project

If a task directory in your repository contains files named amper and module.yaml or project.yaml, it is an Amper project. The procedure for working with such projects is simple.

  1. Start IntelliJ. On the Welcome screen, click the Open button.

  2. Select a directory containing an Amper project (e.g., task1_5). A dialog will appear, asking whether you trust the project. Click the Trust Project button.

  3. Wait for the project to import. This might take a while!

When the main window appears, it should show the README file associated with the task.

To add code to an Amper project, navigate down to the src directory in the project panel on the left of the screen. Right-click on this directory and select New > Kotlin Class/File. Choose the appropriate file type from the list of options, enter a name for the file, then press Enter.

IntelliJ should create a run configuration for the project automatically. You should therefore be able to use the Run button (the one with a green triangle icon at the top of the window) to run the application.

Unfortunately, Amper isn’t as neatly integrated into IntelliJ as Gradle, particularly when it comes to handling tests. To run the unit tests in an Amper project, you will need to open a terminal, either using the button in the toolbar on the left of the window, or by selecting View > Tool Window > Terminal. Then, in the terminal, enter

./amper test