IntelliJ IDEA 2016.3 Help

Tutorial: Debugging Your First Java Application

On this page:

Before you start...

You have already created and executed your first Java application. Now it's time to debug it.

However, it would be nice to add one more line to the application - let it be

System.out.println("it's me, Wombat!");

Putting breakpoints

To start a debugger session, first of all you need to place a breakpoint at the statement where you want to suspend the execution of your application. The existing source code does not give you much of a choice - the only place, where you can put breakpoints, are the statements

System.out.println("Hello World!"); System.out.println("it's me, Wombat!");

So let's do it. Click the left gutter at the line of the statement you want to put a breakpoint on, or just press Ctrl+F8:

/help/img/idea/2016.3/HWDebug_create_breakpoint.png

As you see, the new breakpoint is added to the source code. The line where the breakpoint is set changes its color to pink.

If you just hover your mouse pointer over the breakpoint, you will see its properties in the tooltip:

/help/img/idea/2016.3/HWDebug_view_breakpoint.png

Suppose you want to change some properties of this breakpoint. Then right-click it and see the following dialog box:

/help/img/idea/2016.3/HWDebug_breakpoint_properties.png

Finally, you would like to explore and change all the available properties of a breakpoint and see its location among the other breakpoints (if any). In this case, press Ctrl+Shift+F8:

/help/img/idea/2016.3/HWDebug_breakpoint_properties_full.png

Starting a debugger session

Now that the breakpoints are added, you can debug your application. This can be done in numerous ways; however, let's follow the easiest one.

Mind the icon /help/img/idea/2016.3/left_gutter_run.png that marks a class with the main() method. Clicking this icon reveals a menu that enables running and debugging such a class:

/help/img/idea/2016.3/HWDebug_start_session.png

What does it mean?

IntelliJ IDEA launched the debugger session with the temporary run/debug configuration. This run/debug configuration has the default name "HelloWorld.main()'. To view and change the settings of this run/debug configuration, choose Run | Edit Configurations... on the main menu:

/help/img/idea/2016.3/ij_runConfigMenu.png

or click the run/debug configuration selector, and then choose Edit Configurations...:

/help/img/idea/2016.3/HWDebug_runconfig.png

IntelliJ IDEA compiles your application (which takes time!), and then suspends the application at the first breakpoint.

The IntelliJ IDEA window now looks different:

  • The first thing that has changed is the color of the first line with the breakpoint. Now this line is rendered blue:
    /help/img/idea/2016.3/HWDebug_breakpoint_hit.png

    It means that (according to the breakpoint properties) the application has reached this breakpoint, hit it and suspended before the statement println.

  • Next, in the lower part of the IntelliJ IDEA window, a special tool window has emerged. This is the Debug tool window that features the stepping toolbar and shows all the necessary information you might need during the debugger session:
    /help/img/idea/2016.3/HWDebug_toolwindow.png

Stepping through the application

Stepping through the statements directly

Let's step through the application. Click /help/img/idea/2016.3/frames_step_over.png on the stepping toolbar, or just press F8.

The next line now becomes blue. If you look at the Debug tool window, you notice the following changes:

  • In the Frames pane, the next line number is shown.
  • The Console tab is marked with the icon /help/img/idea/2016.3/console_info_icon.png, which means that it contains new output.
/help/img/idea/2016.3/HWDebug_breakpoint2_hit.png

Click the Console tab. You see the message of the first line with the breakpoint "Hello, World!". The second message is not yet visible:

/help/img/idea/2016.3/HWDebug_console.png

Click /help/img/idea/2016.3/frames_step_over.png again on the stepping toolbar, or press F8. Now the second message appears in the console. After you click /help/img/idea/2016.3/frames_step_over.png the next time, the application stops:

/help/img/idea/2016.3/HWDebug_console2.png

This debugging session is over.

Stepping through the method calls

Now let's explore a more complicated way and step into the println() call.

First, restart the debugger session. To do that, just click /help/img/idea/2016.3/debug.png on the toolbar of the Debug tool window. Thus you'll rerun the latest run/debug configuration, namely, HelloWorld.

The application again pauses at the first breakpoint. This time, click /help/img/idea/2016.3/frames_force_step_into.png, or press Shift+Alt+F7. You see a different picture:

/help/img/idea/2016.3/HWDebug_step_into_class.png

It means that IntelliJ IDEA has stepped into the method println(String x) of the library class PrintStream.java. Note that a new thread appears in the list of threads.

Click /help/img/idea/2016.3/frames_step_out.png or press Shift+F8 to return to the next breakpoint:

/help/img/idea/2016.3/HWDebug_breakpoint3_hit.png

Note that the Console tab has again got the marker /help/img/idea/2016.3/console_info_icon.png, which means that new output is available. Next, click /help/img/idea/2016.3/frames_step_over.png. You see that the process terminates:

/help/img/idea/2016.3/HWDebug_application_stop.png

See Also

Procedures:

External Links:

Last modified: 29 November 2016