IntelliJ IDEA 14.0.0 Web Help

Creating and Running Your First Java Application

To get an impression of how IntelliJ IDEA can help you develop and run Java applications, you can start by creating, building and running the age-old "Hello, World" example. By doing so, you'll be able to learn about the IDE features without the need to concentrate on coding.

Before you start

To develop Java applications, you need a Java Development Kit (JDK). So, the first thing to do is to make sure that you have a JDK installed.

If necessary, you can download an Oracle JDK from the Java SE Downloads page. Installation instructions can be found there as well.

Creating a project

Any new development in IntelliJ IDEA starts with creating a project. So let's create one now.

  1. If no project is currently open in IntelliJ IDEA, click Create New Project on the Welcome screen. Otherwise, select New Project from the File menu.

    As a result, the New Project wizard opens.

  2. In the left-hand pane, select Java. (We want a Java-enabled project to be created, or, to be more exact, a project with a Java module.)
    HWJ000NewProjectJava
  3. If you haven't defined a JDK in IntelliJ IDEA yet (in such a case <None> is shown the Project SDK field), you should do it now.

    Click New and select JDK.

    HWJ002NewProjectNewJDK

    In the Select Home Directory for JDK dialog that opens, select the directory in which you have the desired JDK installed, and click OK.

    HWJ003SelectHomeDirForJDK

    The JDK you have specified is shown in the Project SDK field. (This JDK will be associated by default with all projects and Java modules that you will create later.)

    HWJ004NewProjectJDKSet

    Because our application is going to be a "plain old Java application", we don't need any "additional" technologies to be supported. So, don't select any of the options under Additional Libraries and Frameworks.

    Click Next.

  4. The options on the next page have to do with creating a Java class with a main() method.

    Since we are going to study the very basics of IntelliJ IDEA, and do everything "from scratch", we don't need these options at the moment. In other circumstances, they may, of course, be very useful.

    So, don't select any of the options.

    HWJ001NewProjectFromTemplate

    Click Next.

  5. On the next page, specify the project name (e.g. HelloWorld). If necessary, change the project location suggested by IntelliJ IDEA.
    HWJ002NewProjectJavaModuleProjectName

    Click Finish.

    Wait while IntelliJ IDEA is creating the project. When this process is complete, the structure of your new project is shown in the Project tool window.

Exploring the project structure

Let's take a quick look at the project structure.

HWJ006ProjectToolWindowInitialState

There are two top-level nodes:

  • HelloWorld. This node represents your Java module. The .idea folder and the file HelloWorld.iml are used to store configuration data for your project and module respectively. The folder src is for your source code.
  • External Libraries. This is a category that represents all the "external" resources necessary for your development work. Currently in this category are the .jar files that make up your JDK.

(For more information, see IntelliJ IDEA Tool Windows and Project Tool Window.)

Creating a package and a class

Now we are going to create a package and a class. Let the package and the class names be com.example.helloworld and HelloWorld respectively.

Though a package can be created separately, we will create both the package and the class at once.

  1. In the Project tool window, select the src folder and press Alt+Insert. (Alternatively, you can select File | New, or New from the context menu for the folder src.)
  2. In the New menu, select Java Class (e.g. by pressing Enter).
    HWJ010ProjectToolWindowNewClass
  3. In the Create New Class dialog that opens, type com.example.helloworld.HelloWorld in the Name field. The Class option selected in the Kind list is OK for creating a class. Press Enter to create the package and the class, and close the dialog.
    HWJ011CreateNewClassName

    The package com.example.helloworld and the class HelloWorld are shown in the Project tool window.

    HWJ011NewClassInProjectToolWindow

    At the same time, the file HelloWorld.java (corresponding to the class) opens in the editor.

    HWJ012NewClassInEditor

Note the package statement at the beginning of the file and also the class declaration. When creating the class, IntelliJ IDEA used a file template for a Java class. (IntelliJ IDEA provides a number of predefined file templates for creating various file types. For more information, see File and Code Templates.)

Also note a yellow light bulb intentionBulb. This bulb indicates that IntelliJ IDEA has suggestions for the current context. Click the light bulb, or press Alt+Enter to see the suggestion list.

HWJ013IntentionActionsSuggestionList

At the moment, we are not going to perform any of the actions suggested by IntelliJ IDEA (such actions are called intention actions.) Note, however, that this IntelliJ IDEA feature may sometimes be very useful.

Finally, there are code folding markers next to the commented code block (foldingMinus). Click one of them to collapse the corresponding block. (For more information, see Code Folding.)

HWJ014CommentedCodeFolded

Writing code for the HelloWorld class

The code in its final state (as you probably know) will look this way:

package com.example.helloworld; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }

The package statement and the class declaration are already there. Now we are going to add the missing couple of lines.

Press Shift+Enter. (In contrast to Enter, Shift+Enter starts a new line without breaking the current one.)

HWJ015NewLine

Using a live template for the main() method

The line

public static void main(String[] args) {}

may well be typed. However, we suggest that you use a different method. Type p and press Ctrl+J.

HWJ016psvm

Select psvm - main() method declaration. (Use the Up and Down arrow keys for moving within the suggestion list, Enter for selecting a highlighted element.)

Here is the result:

HWJ017MainMethodInserted

(IntelliJ IDEA provides code snippets called live templates. psvm is an abbreviation for one of such templates. To insert a live template into your code, you can use Code | Insert Live Template or Ctrl+J. For more information, see Live Templates.)

Using code auto-completion

Now, it's time to add the remaining line of code

System.out.println("Hello, World!");

We'll do that using code auto-completion.

Type Sy

The code completion suggestion list is shown.

HWJ018InsertSystem

Select System (java.lang) by pressing Enter.

Type .o and press Ctrl+Period.

HWJ020InsertOut

out is inserted followed by a dot. (You can select an item in the suggestion list by pressing Ctrl+Period. In that case, the selected item is inserted into the editor followed by a dot.)

Type p and then find and select println(String x).

HWJ022InsertPrintLn

IntelliJ IDEA prompts you which parameter types can be used in the current context.

HWJ023PrintLnInserted

Type "

The second quotation mark is inserted automatically and the cursor is placed between the quotation marks. Type Hello, World!

HWJ024CodeReady

The code at this step is ready.

(For more information, see Auto-Completing Code.)

Using a live template for println()

As a side note, we could have inserted the call to println() by using a live template (sout).

If you think that it's enough for live templates, proceed to running the application. Otherwise, try that now as an additional exercise. Delete

System.out.println("Hello, World!");

Type s, press Ctrl+J and select sout - Prints a string to System.out.

The line

System.out.println();

is added and the cursor is placed between ( and ).

Type " and then type Hello, World!

Building and running the application

Classes with a main() method can be run right from the editor. For this purpose, there is a Run '<ClassName>.main()' command in the context menu.

Right-click somewhere in the editing area and select Run 'HelloWorld.main()'.

HWJ028RunHelloWorld

Wait while IntelliJ IDEA is compiling the class. When the compilation is complete, the Run tool window opens at the bottom of the screen.

HWJ029RunToolWindow

On the first line, there is a fragment of the command that IntelliJ IDEA used to run the class. (Click the fragment to see the whole command line including all options and arguments.) The last line shows that the process has exited normally, and no infinite loops occurred. And, finally, you see the program output Hello, World! between these lines.

(For more information, see Run Tool Window.)

Remarks

This is, basically, it. However, there are final remarks worth making related to building and running applications in IntelliJ IDEA.

  • Prior to running the class, IntelliJ IDEA has automatically compiled it. When necessary, you can initiate the compilation yourself. The corresponding options can be found in the Build menu.
    HWJ025BuildMenu

    Many of these options are also available as context menu commands in the Project tool window and in the editor.

    Finally, there is an icon in the upper-right part of the workspace that corresponds to the Make Project command (build).

    HWJ025BuildIcon

    For more information, see Build Process and Compilation Types.

  • The options for running and debugging applications can be found in the Run menu.
    HWJ030RunMenu

    As in the case of the build operations, the run options can also be accessed from the Project tool window and the editor, as well as by means of controls in the upper-right part of the workspace.

    HWJ031RunToolbarButtons
  • Applications in IntelliJ IDEA are run according to what is called run/debug configurations. Such configurations, generally, should be created prior to running an application.

    When you performed the Run 'HelloWorld.main()' command, IntelliJ IDEA, first, created a run configuration and then executed it.

    The name of this run configuration (HelloWorld) is now shown in the run/debug configuration selector to the left of run.

    The HelloWorld configuration now exists as a temporary configuration and, if necessary, you can save it to make it permanent.

    HWJ032RunConfigSelectorMenu
  • Run/debug configurations can do a lot more than just run applications. They can also build applications and perform other useful tasks.

    If you look at the settings for the HelloWorld run configuration (Run | Edit Configurations or Edit Configurations from the run configuration selector), you'll see that the Make option is included by default in the Before launch task list. That's why IntelliJ IDEA compiled the class when you performed the Run 'HelloWorld.main()' command.

    HWJ033RunConfigSettings

    For more information, see Running, Debugging and Testing.

  • If you look at the Project tool window, you'll see that now there is the folder out there. This is the project compilation output folder.
    HWJ026CompilationCompleted

    Inside it is the module output folder (production\HelloWorld), the folder structure for the package com.example.helloworld and the compiled class file HelloWorld.class.

    HWJ027HelloWorldInExplorer

    For more information, see Configuring Project Compiler Output and Configuring Module Compiler Output.

Last modified: 12 November 2014
comments powered by Disqus