Worksheet 1: 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 2022.3, on your own PC1. 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
    • Choose a value in the range 2-4 and use it for ‘Tab size’, ‘Indent’ and ‘Continuation 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. Enter a name for the project, such as ex1, and a directory where you want the project directory to be located.

    Leave the options to ‘Create Git repository’ and ‘Add sample code’ unchecked. Make sure that ‘Java’ and ‘IntelliJ’ are selected as the Language and Build System, respectively.

    Finally, select a JDK version from the ‘JDK’ drop-down list. Note that 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.

    Click the Create button to create the project directory and open the project in IntelliJ.

    New Project dialog of the IntelliJ IDE
  2. 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.

    In the project tool window, you will see 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 window after creation of a Java class
  3. 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 in the code, etc.

Running Java Programs

  1. To run your finished program, either select RunWeight.java 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. For some exercises, you may be required to configure how a program is being run - e.g., to specify command line arguments. You can do this by creating a run configuration. Select RunRun… from the toolbar and then click on the ‘Edit configurations’ link.

    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.

Finding Out More

Consult the user manual to learn more about IntelliJ.

IntelliJ also has a built-in interactive training facility that can you use to gain experience with more of its features. You can access this by selecting Learn from the start screen.

Accessing IntelliJ's interactive training

  1. An old version is available on SoC Linux machines, but we do not recommend that you use it, and this worksheet will not accurately describe that version! ↩︎