Worksheet 2: Developing With a Java IDE

Java software can certainly be written using an ordinary programmer’s text editor, but Java is a fairly complex, verbose language. Integrated Development Environments (IDEs) have a variety of features that help you deal with this complexity and speed up development.

This worksheet provides a basic introduction to IntelliJ IDEA, a widely-used Java IDE, hereafter referred to simply as IntelliJ.

Acquiring IntelliJ

We assume here that you will be using IntelliJ version 2021.3. You can download it from https://www.jetbrains.com/idea/download/.

You can use either the Community Edition or Ultimate Edition on your PC, for free. If you opt for the Ultimate Edition you will need a student licence, which you can obtain simply by completing an application form. Note that this licence will allow you to use the full professional versions of other JetBrains tools, which you can use to work with C/C++, Python, databases, etc.

For more information on using IntelliJ, please see the user manual.

Configuring IntelliJ

It is important to spend some time configuring a development environment so that it works the way you want it to. Follow the steps below to see how some aspects of IntelliJ’s behaviour can be configured.

  1. From the start screen, click on Customize from the menu on the left. Choose your preferred IDE theme (dark and light options are available) and select your preferred font sizes for the IDE and code editing.

  2. Next, click on the All settings… link. Open up the Appearance & Behaviour section of the menu on the left of the Preferences dialog and click on Appearance.

    Scroll down to the ‘Tool Windows’ section and tick the checkboxes for ‘Show tool window bars’ and ‘Show tool window numbers’. Click on the Apply button at the bottom right of the dialog to apply these changes. (Note: we assume that you’ll do the same to apply changes after each of the configuration steps outlined below.)

  3. Navigate to EditorGeneralAppearance in the Preferences list. Tick the checkboxes for ‘Show line numbers’ and ‘Show indent guides’.

  4. Navigate to EditorInspections. This specifies preferences for how IntelliJ inspects and and advises on the quality of your code. For now, we want to reconfigure just one of these inspections.

    Expand JavaDeclaration Redundancy from the list of inspections and untick the checkbox for the ‘Declaration access can be weaker’ inspection. This will stop IntelliJ from issuing unnecessary warnings about public class and method definitions.

  5. Now navigate to EditorCode Style. Enter 80,100 in the box labelled ‘Visual guides’. This will put vertical guidelines at 80 and 100 columns in the editing window, which will help to warn you about excessively long lines of code.

  6. Finally, navigate to EditorCode StyleJava. Spend some time exploring the various settings. You should be able to see their effect in the sample code displayed in the dialog.

    Please make sure that you

    • Leave the ‘Use tab character’ option unchecked
    • Use values in the range 2-4 for ‘Tab size’ and ‘Indent’
    • Use settings that leave 1 or 2 blank lines between methods of a class
    IntelliJ code style settings dialog for Java

    Click on the OK button at the bottom right to apply any remaining changes and dismiss the dialog.

Creating a Java Project

Let’s assume you wish to work on a solution to Exercise 1, using IntelliJ.

  1. Go back to the start screen and click the New Project button. Make sure that ‘Java’ is selected in the list of project types on the left of the New Project dialog. Select a JDK version from the ‘Project SDK’ drop-down list, then click the Next button.

    Note: there may be multiple options available here. IntelliJ will try to detect installed JDKs automatically, but you can also add a JDK manually using the Add JDK… menu item. You can even download a JDK from here if you need to.

    New Project dialog of the IntelliJ IDE
  2. On the next page of the New Project dialog, leave the option to ‘Create project from template’ unchecked, and click Next again. This will take you to a screen where you need to select a location for the project.

    Click the ‘…’ button to open a file chooser dialog, browse to your repository and select the ex1 subdirectory, then click Open. This should fill in the Project Location textbox with the full path to the ex1 subdirectory, and also fill in ex1 as the name of the project. Change the project name if you want, then click Finish.

  3. After a short delay, you should see the normal IntelliJ interface appear, with the project tool window on the left and the editing area on the right. The latter will be showing the Markdown-formatted README file that was already in the ex1 directory.

    IntelliJ window after creation of the Exercise 1 project
  4. In the project tool window, notice that IntelliJ has created a folder named src. This is where you should create the .java file needed for the exercise. Right-click on the src folder and choose NewJava Class. In the pop-up dialog, enter Weight as the name of the class and press Enter.

    IntelliJ will create a Weight.java file, containing an empty class definition. It will also invite you to perform a git add operation to stage the new file. Click the Add button to d this.

    IntelliJ dialog used for adding new files to version control
  5. With the cursor positioned within the class definition, type main and press the Tab key. IntelliJ will expand this to a fully-defined but empty main() method, within which you can write the required code.

    As you type in the code, you’ll find that IntelliJ will help you in various ways – e.g., by suggesting possible code completions, adding import statements for classes you are using the code, etc.

Running Java Programs

  1. To run your finished program, either select RunRun… from the toolbar, or click on one of the small green triangles in the editor margin. Doing the latter will pop up a menu, from which you can choose to either run or debug the program.

  2. After compiling the program, IntelliJ will make the run tool window visible. You can type input into this window and see the resulting program output. You can also rerun the program by clicking on the small green triangle in the run tool window.

    IntelliJ's Run tool window
  3. Now check the toolbar at the top of the IDE. You should see that IntelliJ has created a run configuration for the program. You can create multiple run configurations within a single project and then choose the one you want from the drop-down list. Clicking on the green triangle in the toolbar will run the currently selected configuration.

Using Git

Whenever you create a project within a Git repository, as we’ve done here, IntelliJ recognizes this and offers support for running Git commands. It’s a good idea to familiarize yourself with the various ways of invoking these commands.

  1. Examine the options available from the Git menu on the toolbar.

  2. Click on the Commit tab on the left-hand side of the IDE. This will replace the project tool window with the commit tool window. (You can also switch to this window by pressing Alt+0 on Linux/Windows, or ⌘+0 on Mac.) Tick the checkboxes for all of the changes that you want to include in this commit, then type a suitable commit message in the text box and click the Commit button1.

    IntelliJ's commit tool window
  3. Trigger the VCS Operations pop-up menu, by pressing Ctrl+V on a Mac, or Alt+` (backtick) on Linux/Windows. This is a quick and convenient way of accessing some Git commands.

    IntelliJ's VCS Operations pop-up menu

    Select Push… from this menu, or use one of the alternative methods:

    • Click on the green ↗ button on the toolbar

    • Press Ctrl+Shift+K on Linux/Windows

    • Press ⇧⌘K on a Mac

    This will take you to the Push Commits dialog, where you can review any unpushed commits and then push them up to GitLab. You might be prompted to enter your SSH passphrase at this point. Wait a few seconds, then browse your repository on gitlab.com to check that the commit has been pushed successfully.

  4. Activate the Git tool window by clicking on the tab on the bottom edge of the IDE, or by pressing Alt+9 on Linux/Windows, ⌘+0 on Mac. You can use this window to view the commit history or execute various other Git commands – e.g., viewing a diff of the changes made in a particular commit.

    IntelliJ's Git tool window, showing commit history

Finding Out More

Consult the user manual to learn more about IntelliJ.

You may also find the YouTube channel useful – in particular the New Project Wizard and Creating Your First Java Application videos.


  1. Alternatively, you could click Commit and Push… here. However, in this case, we are going to do the commit and push operations separately. ↩︎