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.

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. Stomach for Java


Install the Java Extension Pack

There are 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 theExtensionsview (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.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_4.png

Editing and Navigating Code

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_5.gif

Rolled

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, useQuick Open(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 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 useQuick Open(Ctrl+P) then enter the ‘#’ command to search the current workspace. 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 Alt+F12. Alternatively, you can choosePeek Definitionfrom the context menu (right-click, then choosePeek 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 chooseGo to Definitionfrom the context menu (right-click, then chooseGo 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
  • CodeLens (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- IncludingPause,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- TheCALL STACKpanel 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.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_6.png

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 theDebugview (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. ClickStartbutton or press
    F5

    to start debugging.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_7.gif

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.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_8.png

Your test cases are identified automatically.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_9.gif

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.

Writing,_running,_and_debugging_Java_applications_using_Visual_Studio_Code_(vscode)_10.gif

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.