Running and debugging Java

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:

The Java debugger is an open-source project, which welcomes contributors to collaborate through GitHub repositories:

If you run into any issues when using the features below, you can contact us by clicking the Report an issue button below.

Install

For the debugger to work, you also need to have the Language Support for Java(TM) by Red Hat extension installed. To make it easier, there is a Java Extension Pack, which bundles the Language Support for Java(TM) by Red Hat, the Debugger for Java and several other popular Java extensions.

You can manually install the extension pack from the Extensions view (kb(workbench.view.extensions)) by typing vscode-java-pack in the search box. You will also be prompted to install the Java Extension Pack when you edit a Java file in VS Code for the first time.

Use

It's easy to run and debug your Java application as there are several entry points for starting a debugging session.

CodeLens

Once the debugger is activated, you will find Run|Debug on the CodeLens of your main() function.

CodeLens

You can also disable the CodeLens if you prefer, with the Enable Run Debug CodeLens setting. While CodeLens is disabled, you can still access the Run|Debug actions by hovering over your main() function.

Context menu

Another way to start debugging is to right-click a Java file in the File Explorer or editor and select Run or Debug in the context menu.

ContextMenu

Pressing F5

Once you click Run on the CodeLens or press kb(workbench.action.debug.start), the debugger will automatically find the entry point of your project and start debugging. You can also start a debugging session from the Run menu or the Run view opened by the Run icon in the Activity Bar on the side of VS Code. See more at Debugging in VS Code.

It's possible that there might be multiple debugging configurations for your project and you can always add and modify those, then select the desired one to run.

If there's no debug configuration file launch.json in your project, the debugger will automatically find the main class and generate the configuration for you to launch your application. VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings. For more details, please read Launch configurations.

By default, the Java debugger doesn't persist the launch.json in your workspace. If you would like to save it, you can click the create a launch.json file link in the Run view where you will also find the Debug and Run buttons.

Debug Menu

There's also a convenient setting for debugging current file, so the editor knows which file is currently active and choose it as the entry point.

Debugging single files

VS Code can run and debug single Java files without any project.

Debugging external files

The Java debugger also supports external source files. This lets you debug third-party classes when they are inside a JAR or a source attachment. Set your breakpoints in those classes before you start debugging. You can also attach missing source code with a zip/jar file using the Context menu Attach Source action.

Java 9 and newer versions are supported with VS Code Java Debugger as well.

Threads

You can see all the running threads in the Call Stack pane and work with individual thread using the context menu.

ContextMenu

Debug session inputs

The default Debug Console in VS Code doesn't support inputs. If your program need inputs from a terminal, you can use the Integrated Terminal (kb(workbench.action.terminal.toggleTerminal)) within VS Code or an external terminal to launch it.

Step filtering

Step filter is supported by the extension to filter out types that you do not wish to see or step through while debugging. With this feature, you can configure the packages to filter within your launch.json so they could be skipped when you step through.

Expression evaluation

The debugger also lets you evaluate expressions in the WATCH window as well as the Debug Console. You can also use this feature for conditional breakpoint setting.

Currently, the VS Code Java Debugger uses the Integrated Terminal as the default console, which doesn't support expression evaluation. In order for the console to use this feature, you need to change the console to use the Internal Console in launch.json.

"console": "internalConsole"

If you'd like to use that setting each time you launch a Java program, you can configure it as a global user setting with java.debug.settings.console.

Conditional breakpoint

With the help of expression evaluation, the debugger also supports conditional breakpoint. You can set your breakpoint to break when expression evaluates to true.

Data Breakpoint

You can have the debugger break when a variable change 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.

Data Breakpoint

Hot Code replacement

Another advanced feature the debugger supports is 'Hot Code' replacement. Hot code replacement (HCR) is a debugging technique whereby the Java debugger transmits new class files 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 replacement. The possible setting values are:

Logpoints

Logpoints is also supported by Java Debugger. Logpoints allow you to send output to debug console without editing code. They're different from breakpoints because they don't stop the execution flow of your application.

Configuration

There are many options and settings available to configure the debugger. For example, configuring the current working directory (cwd) 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 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

Attach

User Settings

Troubleshooting

If you encounter issues when using the debugger, a detailed troubleshooting guide can be found in the vscode-java-debug GitHub repository.

Common issues explained include:

Feedback and questions

You can find the full list of issues at Issue Tracker. You can submit a bug or feature suggestion and participate in the community driven vscode-java-debug Gitter channel.

Next steps

Read on to find out about:

And for Java: