How to Run Java in Visual Studio Code
Using VS Code to Debug Java Applications
September 28, 2017 Xiaokai He
For Java developers on Visual Studio Code, the Language Support for Java™ by Red Hat extension has been great for providing language features such as IntelliSense and project support. At the same time, we've also heard feedback that users would also like Java debugging. Today, we're excited to announce our ongoing collaboration with Red Hat and enabling Java developers to debug Java applications with a new lightweight Debugger for Java based on Java Debug Server.
To help Java developers to get started with VS Code quickly, we also created an Extension Pack for Java which includes both the Language Support for Java™ by Red Hat and Debugger for Java extensions so you won't need to search for them individually. This is just the start to creating a modern workflow for Java and we'll be adding more features and extensions to the Extension Pack for Java in the future.
Getting started
To get started:
- In Visual Studio Code, open the Extensions view ( ⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- Type "java" to filter the list.
- Find and install the Extension Pack for Java or standalone Debugger for Java extension if you already have Language Support for Java™ by Red Hat installed.
You also install the Extension Pack for Java directly from this blog post:
Install the Extension Pack for Java
After reloading VS Code, open a folder that contains a Java project and follow below steps:
- Prepare the project. Open a
.java
file, and the Java extensions will be activated. Maven, Gradle and Eclipse projects are supported. Project build is automatically handled by the extensions, no need to be triggered manually. - Start debugging. Switch to Run view ( ⇧⌘D (Windows, Linux Ctrl+Shift+D)) and open
launch.json
to add a debug configuration for Java. - Fill in the
mainClass
forLaunch
setting orhostName
andport
forAttach
. - Set your breakpoint and hit
F5
to start debugging.
Supported features
In this release, we support the following features:
- Launch/Attach - You can either launch the Java project within VS Code or attach to any running JVM process in debug mode, locally or remotely.
- Breakpoints - Conditional breakpoints by Hit Count is supported and can easily be set using the inline breakpoint settings window. This allows you to conveniently add conditional breakpoints to your code, directly in the source viewer, without requiring a modal window. Break on exceptions is also supported.
- Control flow - Including Pause, Continue
F5
, Step overF10
, Step intoF11
, Step outShift+F11
- Data inspection - When you're stopped at a breakpoint, the debugger has access to the variable names and values that are currently stored in memory. Inspect/Watch/Set Variables are supported.
- Diagnostics - The CALL STACK panel shows the call stack of your program and allows you to navigate through the call path of each captured allocation. Multi-threaded debugging is supported by parallel stacks.
- Debug Console - The Debug Console lets you see information from both stdout and stderr.
Next Steps
- Checkout more about Java on VS Code
Feedback
Please share your feedback and ask questions to help us improve. You can contact us on Gitter.
Xiaokai He, @JavaOnAzure
How to Run Java in Visual Studio Code
Source: https://code.visualstudio.com/blogs/2017/09/28/java-debug