This is a WIP community resource, containing contribution from community members.
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.
To recall how to use a specific feature, you can refer to the sections below.
Ensure you have followed our guide on Preparing VS Code for Java.
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. Remember to remove breakpoints after you are done debugging that section, so they do not interfere when debugging other parts of the code later.
Video segment 4.57 - 6.04 :
More info from VS Code Docs is here.
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: Here are some ways to launch the Debugger:
Debug Java
main
method and that the Java debugger extension is installed and active.Debug
option right above main
methodF5
| Fn+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 4.34 - 5.05 :
More info from VS Code Docs is here.
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:
[image credit: VS Code Docs]
Video segment 8.19 - 8.47 :
More info from VS Code Docs is here.
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 (/ F10
| Fn+F10
)
[image credit: VS Code Docs]
Video segment 8.00 - 8.19 :
More info from VS Code Docs is here.
Purpose: To enter a method being called on the current line to see how it behaves
How: Click the Step Into button (down arrow) (/ F11
| Fn+F11
)
[image credit: VS Code Docs]
More info from VS Code Docs is here.
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) (/ Shift+F11
| Fn+Shift+F11
)
[image credit: VS Code Docs]
More info from VS Code Docs is here.
Purpose: To pause the execution at a certain breakpoint only when a certain condition is met (E.g. when i == 100)
How:
[image credit: VS Code Docs]
More info from VS Code Docs is here.
Purpose: To pause the execution at a certain breakpoint when a variable changes its value
How:
[image credit: VS Code Docs]
More info from VS Code Docs is here.
Purpose: Logpoints are useful for printing debug messages without modifying your source code, especially in read-only files or production environments.
print
statements and keeping codebase clean!How:
[image credit: VS Code Docs]
More info from VS Code Docs is here.
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.
Purpose: Allows you to change and recompile code without restarting the debugging session, while paused at a breakpoint
More info from VS Code Docs is here.
Purpose: You can configure the debugger using the options and settings. JVM arguments and environment variables is easily done with launch options.
To customize how your program is launched (e.g. passing arguments or setting environment variables), you can modify the .vscode/launch.json
file.
More info from VS Code Docs is here.
Problem: Debugger won't start, debugger process terminated abruptly
Solution:
java --version
and javac --version
)launch.json
: Correct mainClass path e.g. 'com.example.Main'Problem: Breakpoints not working, code runs but does not pause at breakpoints
Solution:
Clean/Rebuild
in VS Code (/ Ctrl+Shift+P
| Cmd+Shift+P
> "Java: Clean the Java Language Server Workspace").mvn clean compile
/ gradle clean build
Problem: Class not found or Error: "Could not find or load main class"
Solution:
src/main/java
existsCtrl+Shift+P
| Cmd+Shift+P
> "Java: List all Java source paths" to show all source paths recognized by the workspace.Ctrl+Shift+P
| Cmd+Shift+P
> "Java: Reload Projects".If you encounter other issues when using the VS Code debugger, refer to the detailed troubleshooting guide given here.
Contributors: Sulaksha Muthukrishnan (@crmlatte)