Java in VS Code

The Java support in VS 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:

  1. Language Support for Java(TM) by Red Hat
  2. Debugger for Java
  3. Java Test Runner
  4. Maven for Java

Install the Java Extension Pack

There're also other popular Java extensions you can pick for your own needs, including:

  1. Spring Boot Support
  2. Spring Initializr Java Support
  3. Tomcat
  4. CheckStyle

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:

  1. Go to the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
  2. 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.

With Maven for Java, you can generate projects from Maven Archetype, browse through all the Maven projects within your workspace, and execute Maven goals easily from an embedded explorer.

Maven Explorer

Editing and Navigating Code

Java Code Editing

Linting

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.

Intellisense

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.

Peek Definition

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
  • Rename
  • Highlights

Debugging

Debugger for Java is a lightweight Java Debugger based on Java Debug Server. It works with Language Support for Java by Red Hat to allow users debugging Java code using Visual Studio Code (VS Code).

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 Shift+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.
  • Hot Code Replacement - A very useful feature to facilitate experimental development and iterative trial-and-error coding.

Debugging Features

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:

  1. Switch to the Debug view (⇧⌘D (Windows, Linux Ctrl+Shift+D)).
  2. Open launch.json to add a debug configuration for Java.
  3. Fill in the mainClass for Launch setting or hostName and port for Attach.
  4. Click Start button or press F5 to start debugging.

Debugging Java Application

For more debugging related information, please visit Java Debugging.

Testing

With the support from the Java Test Runner extension, you can easily run, debug and manage your Java test cases.

Testing Java Application

Your test cases are identified automatically.

Testing Java Application

Currently the Test Runner supports JUnit4. We're working on JUnit5 and TestNG and will be supporting them soon.

Spring Boot and Tomcat

To further improve your Java productivity in VS Code, there are extensions for most popular frameworks and tools such as Spring Boot and Tomcat created by the community.

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.

Tomcat

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.

Next Steps

Learn more about Java in VS Code

Read on to find out about:

  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
  • Tasks - use tasks to build your project and more
  • Debugging - find out how to use the debugger with your project