Java in Visual Studio Code
Support for Java in Visual Studio Code is provided through a wide range of extensions. Combined with the power of core VS Code, these extensions give you a lightweight and performant code editor that also supports many of the most common Java development techniques. VS Code provides essential language features such as code completion, refactoring, linting, formatting, and code snippets along with convenient debugging and unit test support. VS Code can also integrate with tooling and frameworks such as Maven, Tomcat, Jetty, and Spring Boot. Leveraging the power of Visual Studio Code, Java developers get an excellent tool for both quick code editing and also the full debugging and testing cycle. It's a great choice for your Java work if you're looking for a tool which:
- Is fast, lightweight, free, and open source.
- Supports many other languages, not just Java.
- Helps start your Java journey without installing and learning a complex IDE.
- Provides great microservices support including popular frameworks, container tooling, and cloud integration.
- Offers team-based collaboration features such as Visual Studio Live Share.
- Improves your productivity through IntelliSense and other code-aware editing features.
This article will give you an overview of different capabilities of Visual Studio Code for Java developers. For a quick walkthrough of editing, running, and debugging a Java program with Visual Studio Code, see the Java Tutorial.
Install Visual Studio Code for Java
VS Code is a fast editor and ships with great editing features. Before you begin, you must have the Java SE Development Kit (JDK) on your local environment. Visual Studio Code works with all major Java versions from various vendors up to 13.
To help you get started quickly, we created a special Installer of Visual Studio Code for Java developers. The package can be used as a clean install or an update for an existing development environment to add Java or Visual Studio Code. Once downloaded and opened, it automatically detects if you have the fundamental components in your local development environment, including the JDK, Visual Studio Code, and essential Java extensions. During install, it downloads the stable versions of those tools from trusted online sources and installs them on your system.
Note: The Java Pack Installer is currently only available for Windows. For other OS, don't worry. Everything still works, you just have to install those components (JDK, VS Code and Java extensions) individually. We're working on the macOS version, please stay tuned. The documentation will tell you which extensions to install.
Alternatively, you can also add Java language support to VS Code by installing the popular Java extensions by yourself.
Download VS Code - If you haven't downloaded VS Code yet, quickly install for your platform (Windows, macOS, Linux).
To help set up Java on VS Code, there is a Java Extension Pack, which contains the most popular extensions for most Java developers:
- Language Support for Java(TM) by Red Hat
- Debugger for Java
- Java Test Runner
- Maven for Java
- Java Dependency Viewer
- Visual Studio IntelliCode
There are 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.
NOTE: If you are using VS Code on Windows and want to take advantage of the Windows Subsystem for Linux, see Developing in WSL.
For developers new to Java or new to VS Code, we provide a Getting Started experience. Once you've installed the Java Extension Pack, you can open the Getting Started experience from within VS Code with the Java: Getting Started command from the Command Palette. Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and type "Java: Getting Started".
Java project support
There are three things you must understand to work with Java in VS Code:
- How does VS Code handle Workspaces?
- How does VS Code handle Java?
- How does VS Code handle Workspaces that contain Java?
VS Code Workspaces
In Visual Studio Code, a "Workspace" means a collection of one or more filesystem folders (and their children) and all of the VS Code configurations that take effect when that "Workspace" is open in VS Code. There are two kinds of "Workspaces" in VS Code, "folder workspaces" and "multi-root workspaces".
A "folder workspace" is presented by VS Code when you open a filesystem folder (directory) in VS Code.
A "multi-root workspace" can refer to multiple folders (directories) from disparate parts of the file system and VS Code displays the contents of the folder(s) of the workspace together in the File Explorer. To learn more, see Multi-root Workspaces.
Java in VS Code
In contrast to IDEs such as IntelliJ IDEA, NetBeans, or Eclipse, the concept of a "Java project" is provided entirely by extensions, and is not a core concept in the base VS Code. When working with "Java projects" in VS Code, you must have the necessary extensions installed to work with those project files.
For example, Maven, Eclipse, and Gradle Java projects 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 Archetypes, browse through all the Maven projects within your workspace, and execute Maven goals easily from an embedded explorer. Projects can also be created and managed with the Java Dependency Viewer extension.
Visual Studio Code also supports working with standalone Java files outside of a Java project, described in the Java Tutorial with VS Code.
VS Code Workspaces that contain Java
Assuming the necessary Java extensions are installed, opening a VS Code workspace that contains Java artifacts will cause those extensions to understand those artifacts and present options for working with them.
More details about Java project support can be found in Java Project Management in Visual Studio Code.
IntelliSense is a general term for language features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and third-party modules. VS Code supports code completion and IntelliSense for Java through Language Support for Java(TM) by Red Hat. It also provides AI-assisted IntelliSense called IntelliCode by putting what you're most likely to use at the top of your completion list.
Java in Visual Studio Code also supports source code navigation features such as search for symbol, Peek Definition, and Go to Definition. The Spring Boot Tools extension provides enhanced navigation and code completion support for Spring Boot projects.
There are also other editing related features available for Java, such as refactoring and formatting. To learn more, read Editing Java in Visual Studio Code.
Starting a debugging session is easy, click on the Run|Debug button available at the CodeLens of your
main() function, or press F5. The debugger will automatically generate the proper configuration for you.
Although it's lightweight, the Java debugger supports advanced features such as expression evaluation, conditional breakpoints, and hot code replacement. For more debugging related information, visit Java Debugging.
With the support from the Java Test Runner extension, you can easily run, debug, and manage your JUnit and TestNG test cases.
For more about testing, read Testing Java.
Spring Boot, Tomcat, and Jetty
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 with the extension.
See Tomcat and Jetty Support to learn more about Tomcat and Jetty support with VS Code.
Spring Boot support is provided by Pivotal. There are also Spring Initializr Java Support and Spring Boot Dashboard extensions available from Microsoft to further improve your experience with Spring Boot in Visual Studio Code.
See Spring Boot with VS Code to learn more about Spring Boot support with VS Code.
You may Sign up to follow the latest of Java on Visual Studio Code.
Learn more about Java in VS Code
- Java Tutorial with VS Code
- Code Editing and Navigation
- Java Debugging
- Java Testing
- Java Project Management
- Spring Boot with VS Code
- Tomcat and Jetty Support
Read on to find out more about Visual Studio Code: