Guides for SE student projects »

Caution: VS Code guides section is a work-in-progress, containing content contributed by students.

VS Code - Using the Debugger

This tutorial covers the basics of using VS Code’s debugger for Java features.

  • If you are new to using an IDE-based debugger, we recommend watching the video below for an introduction to the features of VS Code Java debugger.

    Debugging in VS Code


  • To recall how to use a specific feature, you can refer to the sections below.

Prerequisites:

Need help with following prerequisites? Check out our Preparing VS Code for Java guide first.

  • VS Code with the Extension Pack for Java installed
  • Java Development Kit (JDK) installed

Adding breakpoints

Purpose: A breakpoint is a line in the code at which the debugger will pause the execution.

How: Click in the left margin next to the line of code where you want to set the breakpoint. A red dot will appear in the margin.


[image credit: VS Code Docs]

To remove the breakpoint, click the red dot again.
  • The Debugger for Java supports various breakpoints, including line breakpoints, conditional breakpoints, data breakpoints, and logpoints.
  • Different types of breakpoints can be added by right-clicking in the left margin and selecting which type of breakpoint you want to add (line breakpoints, conditional breakpoints, and logpoints).

Video segment 0.30 - 0.50 :

More info from VS Code Docs is here.

Running the code in Debug mode

Purpose: To get VS Code to run the code in the debugger mode, so that the debugger can direct the execution flow as needed by the debugging.

How: There are multiple ways to run and debug your Java application using the extension. One way is to click on the dropdown arrow beside the play icon (top editor title bar) and select Debug Java

  • Alternatively, you can press F5
  • By default, the debugger will run out-of-box by automatically finding the main class and generating a default launch configuration in memory to launch your application.

[image credit: VS Code Docs]

Video segment 0.44 - 1.10 :

More info from VS Code Docs is here.

Examining the state of the suspended programme:

Purpose: To examine values of variables at different steps of the execution

How: Use the Debug Console panel at the bottom of the window. At each breakpoint, you can see:

  • Variables: All local variables and their current values.
  • Watch: You can add specific expressions to observe.
  • Call Stack: Shows the current stack of method calls.
  • Breakpoints: Lists all active breakpoints.

[image credit: VS Code Docs]

Video segment 1.10 - 1.48 :

More info from VS Code Docs is here.

Stepping through code

Purpose: To move through code line by line and observe how it is executed

How: Click the Step Over button (curved arrow) in the debug toolbar, or press F10


[image credit: VS Code Docs]

Video segment 1.54 - 2.09 :

More info from VS Code Docs is here.

Stepping into a method:

Purpose: To enter a method being called on the current line to see how it behaves

How: Click the Step into button (down arrow), or press F11


[image credit: VS Code Docs]

Video segment 2.09 - 2.13 :

More info from VS Code Docs is here.

Stepping out of a method:

Purpose: To finish executing the current method and return to its caller, without stepping through the rest of it.

How: Click the Step out button (up arrow), or press Shift+F11


[image credit: VS Code Docs]

Video segment 2.13 - 2.22 :

More info from VS Code Docs is here.

Setting a Conditional Breakpoint:

Purpose: To pause the execution at a certain breakpoint only when a certain condition is met (E.g. when i == 100)

How:

  • Right-click in the editor margin and select Add Conditional Breakpoint
  • Choose the type of condition you want to set (expression, hit count, or wait for breakpoint)

[image credit: VS Code Docs]

More info from VS Code Docs is here.

Evaluate Expression:

Purpose: Allows you to compute the value of an expression at a specific point during execution, enabling dynamic inspection of variables and data structures.

How: Enter through the WATCH window, or the Debug Console

More info from VS Code Docs is here.


Contributors: Sulaksha Muthukrishnan (@crmlatte)