Java in Visual Studio Code
The Java support in Visual Studio Code is provided through extensions and optimized for lightweight Java projects with a simple, modern workflow. Popular extensions in the Marketplace provide essential language features such as project support, code completion, linting, debugging, testing, code formatting, snippets, as well as tooling integration including Maven and Tomcat.
Download VS Code - If you haven't downloaded VS Code yet, quickly install for your platform (Windows, macOS, Linux).
Install Java Extensions
VS Code is a fast editor and ships with great editing features. Add Java language support to VS Code by installing the popular Java extensions. To help set up Java on VS Code, there is a Java Extension Pack which contains popular extensions we picked for most Java developers:
There're also other popular Java extensions you can pick for your own needs, including:
Thanks to the great Java community around VS Code, the list doesn't end there. You can search for more Java extensions easily within VS Code:
- Go to the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- Filter the extensions list by typing "java".
This document describes some of the key features included in those Java extensions.
Java Project Support
Maven, Eclipse and Gradle Java project are supported through Language Support for Java(TM) by Red Hat, by utilizing M2Eclipse which provides Maven support and Buildship which provides Gradle support through the Eclipse JDT Language Server.
Editing and Navigating Code
A linter is a tool that provides warnings for suspicious looking code. Language Support for Java(TM) by Red Hat provides this feature to report parsing and compilation errors as you type, so you can fix them inside VS Code.
VS Code also supports code completion and Intellisense for Java through Language Support for Java(TM) by Red Hat. Intellisense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules.
Search for symbols
You can search for symbols in the current file or workspace to navigate your code more quickly.
To search for a symbol in the current file, use Quick Open (⌘P (Windows, Linux Ctrl+P)) then enter the '@' command, then enter the name of the symbol you're looking for. A list of potential matches will appear and be filtered as you type. Choose from the list of matches to navigate to its location.
To search for a symbol in the current workspace, start by pressing ⌘T (Windows, Linux Ctrl+T), then enter the name of the symbol. A list of potential matches will appear as before. If you choose a match that was found in a file that's not already open, the file will be opened before navigating to the match's location. Alternatively, you can also use Quick Open (⌘P (Windows, Linux Ctrl+P)) then enter the '#' command to search the current workspace. ⌘T (Windows, Linux Ctrl+T) is just the shortcut for the '#' commands, respectively, so everything works the same.
You can take a quick look at how a symbol was defined by using the Peek Definition feature. This feature displays a few lines of code near the definition inside a peek window so you can take a look without navigating away from your current location.
To peek at a symbol's definition, place your cursor on the symbol anywhere it's used in your source code and then press ⌥F12 (Windows Alt+F12, Linux Ctrl+Shift+F10). Alternatively, you can choose Peek Definition from the context menu (right-click, then choose Peek Definition).
Go to Definition
You can also quickly navigate to where a symbol is defined by using the Go to Definition feature.
To go to a symbol's definition, place your cursor on the symbol anywhere it is used in your source code and then press F12. Alternatively, you can choose Go to Definition from the context menu (right-click, then choose Go to Definition). When there's only one definition of the symbol, you'll navigate directly to its location, otherwise the competing definitions are displayed in a peek window as described in the previous section and you have to choose the definition that you want to go to.
Other code editing features include:
- Code formatting
- Code snippets
- Code outline
- Code lens (references)
- Javadoc hovers
The Java Debugger supports 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 and expression evaluation are 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 over
F10, Step into
F11, Step out
- 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.
- Hot Code Replacement - A very useful feature to facilitate experimental development and iterative trial-and-error coding.
The debugger is able to detect your launch class in most cases, all you need to do is start debugging by pressing
F5. If you'd like to define the debugging configuration yourself, just follow the steps below:
- Switch to the Debug view (⇧⌘D (Windows, Linux Ctrl+Shift+D)).
launch.jsonto add a debug configuration for Java.
- Fill in the
- Click Start button or press
F5to start debugging.
For more debugging related information, please visit Java Debugging.
With the support from the Java Test Runner extension, you can easily run, debug and manage your Java test cases.
Your test cases are identified automatically.
Currently the Test Runner supports JUnit4. We're working on JUnit5 and TestNG and will be supporting them soon.
Spring Boot and Tomcat
The Tomcat extension includes an explorer to easily navigate and manage your Tomcat servers. You can create, start, debug, stop and rename your Tomcat server all with the extension.
See Java Tutorial with VS Code to learn more about Tomcat and basic Java support with VS Code.
Spring Boot support is provided by an extension from Pivotal, who also provide extensions for popular platforms such as Cloudfoundry, Concourse and BOSH. There's also a Spring Initializr Java Support extension so you can create a Spring Boot app from scratch.
See Spring Boot with VS Code to learn more about Spring Boot support with VS Code.
Learn more about Java in VS Code
Read on to find out about: