Running and debugging Java
AI agents: See llms.txt for documentation discovery and navigation. Documentation articles are also available as Markdown by appending .md to the article URL. 🎬 Watch The Story of VS Code! VS Code is now on Instagram, follow @vscode.ig for updates, tips, and more. Rewatch GitHub Copilot Day. Running and debugging Java View as Markdown Ask in VS Code Ask in VS Code Insiders Visual Studio Code allows you to debug Java applications through the Debugger for Java extension.
It's a lightweight Java debugger based on Java Debug Server , which extends the Language Support for Java™ by Red Hat . Here's a list of supported debugging features: Launch/Attach Breakpoints Exceptions Pause & Continue Step In/Out/Over Variables Call Stacks Threads Debug Console Evaluation Hot Code Replace The Java debugger is an open-source project, which welcomes contributors to collaborate through GitHub repositories: Debugger for Java Java Debug Server for Visual Studio Code If you run into any issues when using the features below, you can contact us by entering an issue .
Install To get the complete Java language support in Visual Studio Code, you can install the Extension Pack for Java , which includes the Debugger for Java extension. Install the Extension Pack for Java For details on how to get started with the extension pack, you can review the Getting Started with Java tutorial. Configure 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.
If you would like to customize and persist your launch configuration, you can select the create a launch.json file link in the Run and Debug view. The launch.json file is located in a .vscode folder in your workspace (project root folder). For more details on how to create the launch.json , read Launch configurations ; for more details on configuration options for Java, you can read Configuration options . Run and Debug The debugger extension provides multiple ways to run and debug your Java application.
Run from CodeLens You will find Run|Debug on the CodeLens of your main() function. Run from Editor menu Another way to start debugging is to select Run Java or Debug Java menu from the top editor title bar. Run from pressing F5 Pressing F5 , the debugger will automatically find the entry point of your project and start debugging. You can also start a debugging session from the Run and Debug view from the side bar of VS Code.
See more at Debugging in VS Code . Debugging single files In addition to support for debugging Java projects managed by build tools, VS Code also supports for debugging single Java files without any project. Debug session inputs The default Debug Console in VS Code doesn't support inputs. If your program needs inputs from a terminal, you can use the Integrated Terminal ( ⌃` (Windows, Linux Ctrl+` ) ) within VS Code or an external terminal to launch it.
You can also use the user setting java.debug.settings.console to configure a global console for all Java debug sessions. Breakpoints The Debugger for Java supports various breakpoints, such as line breakpoints, conditional breakpoints, data breakpoints, and logpoints. Breakpoint - Conditional breakpoint With the help of expression evaluation, the debugger also supports conditional breakpoints. You can set your breakpoint to break when the expression evaluates to true.
Breakpoint - Data breakpoint You can have the debugger break when a variable changes its value. Note that the data breakpoint can only be set inside a debug session. This means you need to launch your application and break on a regular breakpoint first. You can then pick a field in the VARIABLES view and set a data breakpoint. Breakpoint - Logpoints Logpoints are also supported by the Java Debugger. Logpoints allow you to send output to the Debug Console without editing code.
They're different from breakpoints because they don't stop the execution flow of your application. Breakpoint - Triggered breakpoints A triggered breakpoint is a breakpoint that is automatically enabled once another breakpoint is hit. They can be very useful when diagnosing failure cases in code that happen only after a certain precondition. Triggered breakpoints can be set by right-clicking on the glyph margin, selecting Add Triggered Breakpoint , and then choosing which other breakpoint enables the breakpoint.
Expression evaluation The debugger also lets you evaluate expressions in the WATCH window as well as the Debug Console. Hot Code Replace Another advanced feature the debugger supports is 'Hot Code' replacement. Hot Code Replace (HCR) is a debugging technique whereby the Debugger for Java transmits the class changes over the debugging channel to another Java Virtual Machine (JVM). HCR facilitates experimental development and fosters iterative trial-and-error coding.
With this new feature, you can start a debugging session and change a Java file in your development environment, and the debugger will replace the code in the running JVM. No restart is required, which is why it's called "hot". Below is an illustration of how you can use HCR with Debugger for Java in VS Code. You may use the debug setting java.debug.settings.hotCodeReplace to control how to trigger Hot Code Replace. The possible setting values are: manual - Click the toolbar to apply the changes (default). auto - Automatically apply the changes after compilation. never - Disable Hot Code Replace.
Step filtering Step filtering is supported by the extension to filter out types that you do not want to see or step through while debugging. With this feature, you can configure the packages to filter within your launch.json so they can be skipped when you step through. Configuration options There are many options and settings available to configure the debugger. For example, configuring the JVM arguments and environment variables is easily done with launch options.
Consult the documentation for the Language Support for Java™ by Red Hat extension for help with setting up your project. For many commonly used setups, there are samples available in VS Code Java Debugger Configuration . The document explains how the Java debugger automatically generates configurations for you, and if you need to modify them, how to do so with the main class, different arguments, environment, attaching to other Java processes, and usage of more advanced features.
Below are all the configurations available for Launch and Attach . For more information about how to write the launch.json file, refer to Debugging . Launch mainClass (required) - The fully qualified class name (for example [java module name/]com.xyz.MainApp) or the java file path of the program entry. args - The command-line arguments passed to the program. Use "${command:SpecifyProgramArgs}" to prompt for program arguments.
It accepts a string or an array of strings. sourcePaths - The extra source directories of the program. The debugger looks for source code from project settings by default. This option allows the debugger to look for source code in extra directories. modulePaths - The modulepaths for launching the JVM. If not specified, the debugger will automatically resolve from current project. $Auto - Automatically resolve the modulepaths of current project. $Runtime - The modulepaths within 'runtime' scope of current project. $Test - The modulepaths within 'test' scope of current project. !/path/to/exclude - Exclude the specified path from modulepaths. /path/to/append - Append the specified path to the modulepaths. classPaths - The classpaths for launching the JVM.
If not specified, the debugger will automatically resolve from current project. $Auto - Automatically resolve the classpaths of current project. $Runtime - The classpaths within 'runtime' scope of current project. $Test - The classpaths within 'test' scope of current project. !/path/to/exclude - Exclude the specified path from classpaths. /pa
The rest of this page is on the live site.