Setting up a project in IntelliJ IDEA

From IntelliJ-Wiki

Jump to: navigation, search

All the development assistance provided by IntelliJ IDEA is available only in the context of a project.

A project defines how the IDE will treat the source code of your application at various development stages, for example, while editing, compiling, testing, or deploying. Settings can be project-specific, for example, the structure of folders, coding style, available technologies, or can be inherited from the IDE settings, for example, SDK configurations, global libraries, version control tools.

Contents

Introduction

This tutorial will walk you step by step through developing a simple Hello application within an IntelliJ IDEA project that contains one Java module.

You will:

  1. Create a one-module Java project from scratch.
  2. Explore and expand the application structure.
  3. Configure the folder below the module content root.
  4. Associate the project sources with a version control tool.
  5. Develop and run a Hello world application.
  6. Set up unit testing in the project.

What This Tutorial Is Not About

This tutorial does not teach you Java programming.

Prerequisites

  1. You are working with IntelliJ IDEA version 9 or higher.
  2. JDK is available on your computer.
  3. The version control tool to use is installed and registered and you have the required access credentials. In this tutorial we will use Git.

Creating a Project

Let’s create a simple project with one Java module using the dedicated New Project wizard.

To start the wizard, choose File | New Project on the main menu or click the Create New Project icon on the Welcome screen (Image 1).

Image 1.

Choosing a Scenario

On the first page of the New Project wizard (Image 2), make sure that the option Create project from scratch is selected.

Image 2.

Specifying Project Name and Location

On the second page of the Wizard (Image 3), specify the name of the project (1) , for example, type hello_world_java, and the project location (2). Select the Create module check box (3), and choose Java Module (4) as the module type. The name of this module will be hello_world_java (5).

Image 3.

Specifying Location for Project Sources

On the third page of the Wizard (Image 4), choose the Create source directory option (1) and accept the default name src (2) for it.

Image 4.

Click Next. If you already have one or several JDKs configured at the IDE level, IntelliJ IDEA brings you to the last, Technologies, page. If IDE does not detect any JDK configurations, you are asked to specify a JDK to use in the module.

Specifying a JDK to Use

This page (Image 5) appears if no JDKs are configured (1) at the IDE level. This means that IntelliJ IDEA does not know the location of any JDK home directories, even if you have several JDKs on your computer. This may happen, for example, when you are creating a Java project for the first time after IntelliJ IDEA installation.

Image 5.

However, right on this page you can specify the home directory of the JDK to use in the current module and project. Click the Configure (2) button and specify the location of the desired JDK in the Select Home Directory for JSDK dialog box that opens. When you click OK, you return to the wizard (Image 6) where the selected JDK is listed as the Project JDK (1):

Image 6.

The specified JDK is now available at the IDE level. IntelliJ IDEA will treat this JDK as the default project JDK for any new project and will not bring you to this page anymore. Say Goodbye to it and click Next.

Technologies Page

IntelliJ IDEA brings you to this page either upon specifying the location for the project sources or after configuring a JDK.

This page is intended for enabling various technologies through the dedicated facets. We are not going to use any technologies, so just click Finish. After that, IntelliJ IDEA creates a hello_world_java project and opens it.

Exploring the Application

To explore our application, we’ll use the dedicated Project tool window (1) that shows (Image 7) the following files and folders:

Image 7.
  • The .idea (2) folder contains a number of subfolders, mainly with internal IntelliJ IDEA information.
  • The empty src (3) folder is intended for source code that implements the functionality of your application.

Note: IntelliJ IDEA version 10 does not show the src folder as long as it is empty.

  • The External Libraries (4) folder contains all the libraries required for development that are located outside the project root.

We will expand this structure with one more folder.

Adding a Folder for Tests

Most likely in the course of development, you will decide to set up unit testing in your project. Let’s create a folder for storing test classes right now.

In the Project tool window, right-click the hello_world_java project root node, point to New and select Directory on the context menu:

Image 8.

Specify the name of the new folder, for example, tests, in the New Directory dialog box (Image 9):

Image 9.

If you now look at the Project tool window (Image 10), you will see that the new tests folder (1) is added to the project tree.

Image 10.

You may also notice that it is marked with a yellow icon while the src folder is marked with a blue one (2). These icons indicate different types of contents. The new tests folder is not marked because IntelliJ IDEA does not know how to treat it. We will tell the IDE that the folder contents are of the type test sources when we pass to configuring folders under the content root.

Configuring the Project

IntelliJ IDEA has configured the basic project structure for you. We will tune the basic configuration at the Project and Module levels. The Project level covers the settings that apply to all the modules of a project, such as general project settings, project libraries, facets, and artifacts. The Module level covers the settings that apply to a particular module.

In IntelliJ IDEA, projects are configured mainly in the Project Structure dialog box (Image 11). To open it, press CTRL+ALT+SHIFT+S or choose File | Project Structure on the main menu.

Image 11.

The Project level configuration settings are grouped under the Project Settings node (2) on the left-hand pane (1) of the Project Structure dialog box. The Module level configuration settings are available under the Module (3) node. The other areas of the page show the details of the item selected in the list.

Exploring the General Project Settings

In the Project Structure dialog box, select Project Settings | Project on the left-hand pane. The right-hand pane shows the settings that apply to the entire project (Image 12):

  • The JSDK version used (1).

Note: To specify another version available in the IDE, select it from the drop-down list.

  • The Java version in the Project language level drop-down list (2). This setting affects the scope of coding assistance provided by IntelliJ IDEA and determines the behaviour of the compiler.
  • The project compiler output (3), that is, the path to the folder to store the compilation results in.
Image 12.

These settings will do for this tutorial, so no changes are required on this page.

Configuring Module Settings

Our project consists of just one Java module. Let’s examine which types of contents are presented in it (Image 13). In the Project Structure dialog box, select Modules on the left-hand pane. The central pane shows only one item because our project contains one module, hello_world_java (1). The tabs on the right-hand pane show the configuration details of this module: module content roots (2), output paths (3), and module libraries (4).

Image 13.

Configuring Folders under the Content Root

To examine how the content folders are configured in the module, open the Sources tab.

The tab presents the module contents from two points of view:

  • On the left-hand side, the module contents are grouped into content roots. Under each content root, the content folders are grouped according to their type: production, test, or excluded.
  • On the right-hand side, the module contents are shown in a project tree. The color of each folder icon indicates the content type the corresponding folder belongs to.

For our project, the tab shows (Image 14):

  • One content root, which is actually the project root C:\Git\hello_world_java (1).
  • The Source Folders list (2) that contains the src folder for Java sources (3). This folder was added during the project creation, and there are no other folders with production sources in our module and project.
Image 14.

However, in the project tree on the right, we can see an unclassified folder tests (4). Let’s tell IntelliJ IDEA to treat it a as a test sources folder. Select the tests folder and click the Test Sources button (5) File:rootTest.png.

Now you can see (Image 15) that IntelliJ IDEA has marked the tests folder in the project tree as test sources (1). Besides, the Test Sources Folders group (2) is added to the list of folders below the content root. This group contains our tests folder (3).

Image 15.

The type assigned to the contents of a folder determines where the results of their compilation will be stored. On the compilation start, IntelliJ IDEA creates an out folder right under the project root. The out folder contains two subfolders:

  • production for compiled Java classes.
  • test for compiled test classes.

The output folders are managed on the Paths tab (Image 17).

Image 17.

As you can see, IntelliJ IDEA has already configured output paths for compilation results:

  • C:\Git\hello_world_java\out\production\hello_world_java for compiled sources (1)
  • C:\Git\hello_world_java\out\test\hello_world_java for compiled tests (2)

These settings will do for our project, so no steps from your side are required on the Paths tab.

We are through with tuning the project structure. Click OK to save the changes and close the Project Structure dialog box.

Specifying the Version Control System to Use

Now it’s time to define the tool that will control versions of the sources in our project. But first we need to enable and configure integration between IntelliJ IDEA and the desired version control tool.

Enabling VCS Integration

IntelliJ IDEA supports integration with a number of Version Control Systems (VCS) by means of the dedicated plugins. Let’s, for example, enable IntelliJ IDEA’s Git integration.

  1. First of all, you need to download and install the desired version control tool according to the VCS-specific instructions. So, you have to download and install Git, and create your Git account, if you haven’t done it before.
  2. Next, check whether the plugin for integrating the required VCS with IntelliJ IDEA is enabled. Choose File | Other Settings | Configure Plugins. The first, Select VCS Integration Plugins, page shows a list of available VCS integration plugins (Image 18). The set of available integration plugins depends on the edition of IntelliJ IDEA you are working with. The Community Edition supports integration with CVS, Git, and Subversion. Integration with Perforce, TFS, StarTeam and other commercial VCSs is available only in the Ultimate Edition.
    Image 18.
    Make sure that the check box next to the required VCS is selected. In our case, select Git (1). If everything is correct, you can just click Cancel (3) and continue with the tutorial. Otherwise, select the relevant check box, click Next (2), pass the entire Plugin Configuration Wizard, and then restart IntelliJ IDEA.
  3. Now you need to tell IntelliJ IDEA where the executable file or .jar file that implements your VCS is. Choose File | Settings or press CTRL+ALT+S to open the Settings dialog box (Image 19).
    Image 19.
    Click Version Control (1) on the left-hand pane, then click VCSs (2) and the <required VCS>, in our case it is Git (3). On the Version Control | VCSs | <Version Control> page, specify the location of the executable or archive file (4) and other VCS-specific configuration settings (5). The configured integration settings will apply to all IntelliJ IDEA projects.

Now you can associate this enabled VCS integration with the project sources. To do this, you even do not need to leave the Settings dialog box.

Putting the Project Sources under Version Control

Let’s have one tool for all source folders, although IntelliJ IDEA provides the possibility to have different folders controlled by different Version Control Systems (VCSs).

In the current,Settings, dialog box (Image 20), click Version Control (1) on the left-hand pane. The Version Control page opens showing that no VCS (5) is assigned to the project root (4). From the VCS drop-down list (6), select the VCS you want to use, for example, Git (7).

Image 20.

Creating a Local Git Repository

The steps described in this section are required only if you are going to use Git in your project. If you decide on another VCS, skip this section.

Technically, Git sets up a local repository by creating a .git folder in the specified parent directory. After that all the files in the directories below this parent can be considered stored in this repository. In our case, let’s create a .git directory right below the project root.

On the main menu, choose Version Control | Git Init.

Image 21.

In the Select directory for git init dialog box that opens (Image 22), choose the project root folder hello_world_java as the location for the .git directory and click OK.

Image 22.

It is suggested that you learn more about setting up a local Git repository and the Git integration in general.


Developing a Hello World Application

Now let’s develop the “obligatory” Hello World application.

Creating a HelloWorld Class

First of all, we need to create a Java class that will implement the application functionality. In the Project tool window (Image 23), right-click the src node, point to New, then choose Java Class on the context menu.

Image 23.

In the New Class dialog box (Image 24), type HelloWorld in the Name text box and click OK.

Image 24.

Note: Depending on the version control system you use, the corresponding VCS integration may suggest putting the new file under control. For more details, see VCS-Specific Procedures.

After that IntelliJ IDEA creates a stub class for you.

Preparing the Source Code

IntelliJ IDEA opens the new stub class in the editor tab (Image 25):

Image 25.

Add the following code to the stub:

     public static void main(String[] argv) throws IOException {
        BufferedReader buff = new BufferedReader(
        new InputStreamReader(System.in));
        System.out.print("What's your name? ");
        System.out.flush();
        String s = buff.readLine();
        System.out.printf("Hello, %s!", s);
    }

As you type, IntelliJ IDEA detects several unresolved references and highlights them red (Image 26.):

Image 26.

But IntelliJ IDEA also suggests quick fixes for the problems, for example (Image 27.):

Image 27.

Accept the suggested fixes by pressing Alt+ENTER. IntelliJ IDEA will generate the following import statements:

     import java.io.BufferedReader;
     import java.io.IOException;
     import java.io.InputStreamReader;

Running the Application

Now, let’s run our application to make sure that it works, before committing the updates to the local repository. To do that, we need a run configuration. Fortunately, IntelliJ IDEA has already created the default hello_world_java configuration. Just press Ctrl+Shift+F10 and IntelliJ IDEA opens the Run tool window (Image 28):

Image 28

Introduce yourself, for example, type Jerry, and press ENTER (Image 29):

Image 29.

IntelliJ IDEA displays the greeting (Image 30):

Image 30.

Congratulations, your application works!

Unit Testing

Now let’s add unit testing in our project to ensure integrity of the source code in the future. Unit testing and test generation is supported through the bundled JUnit or TestNG plugins. Both of them are enabled by default. In this tutorial, we will use JUnit, version 4.

To set up unit testing, we need to generate test classes and store the created tests in a folder which the IDE recognizes as tests in the project structure. When configuring the project structure, we have created a tests folder and marked it as test sources. Let’s store the generated tests in it.

Generating Tests

IntelliJ IDEA provides the dedicated intention action for generating test classes. We will use this facility to generate a test for the HelloWorld class.

Open the HelloWorld.java class in the editor, if it is not opened (Image 31). Position the cursor on the class name, HelloWorld (2), press ALT+ENTER, and choose the Create test option (2) on the context menu.

Image 31.

IntelliJ IDEA opens the Create Test dialog box (Image 32.), where we will specify the methods to generate tests for and the tool to use for test generation.

Image 32.
  • The Generate test methods for list (1) shows the methods of the selected class. Our class has only one method, main, which is on the list. Select the check box next to main(String[] argv): void (2).
  • Select the JUnit4 option (3) in the Testing library area. IntelliJ IDEA informs you that our module does not have the required library (4), but also suggests a solution for the problem. All you have to do is to click the Fix button (5). The required libraries are attached to the module.

Click OK to continue with specifying the folder to store the tests in.

Specifying the Location for Tests

The Choose Destination Directory dialog box that opens (Image 33) shows two folders configured below the content root: src and tests with the corresponding color markers.

Image 33.

Select С:\Git\hello_world_java\tests (1) and click OK. IntelliJ IDEA creates a test class HelloWorldTest.java and opens it in the editor (Image 34). A new editor tab (5) shows the generated test stub (6).

Image 34.

If you now look at the Project tool window (Image 34), you will see that the test class HelloWorldTest.java (1) has been added under in the tests folder (2). Also note that the junit.jar (3) has been attached to the module and shown under the External Libraries node (4).

See Also

On completing this tutorial you have become familiar enough with configuring and populating a project to start development of a real working application. However, there are still some topics you may be interested in:

Personal tools