Visual Studio Code for Java: The Ultimate Guide 2019

DZone 's Guide to

Visual Studio Code for Java: The Ultimate Guide 2019

Here's everything you need to know about VS Code.

· Java Zone ·
Free Resource

Image title

Visual Studio Code has come a long way to become the preferred text editor of polyglot developers. Javascript, TypeScript, Go, Python, and other languages have a massive amount of developers coding through VS Code, in part thanks to the huge ecosystem of extensions that enhance and enrich the experience in VS Code, turning it into a super smart text editor with IDE-ish capabilities while remaining ludicrously fast and lightweight.

Unsurprisingly, VS Code can do Java as well, and many Java champions and speakers have chosen it for their on-stage appearances and live demos.

Here's the ultimate guide of 2019 for Java developers to use Visual Studio Code to develop, run, debug, and deploy their applications.

If you haven't already downloaded Visual Studio Code, install it now. The rest of this guide will assume you have at least Java 8 installed, though it works with Java 11, too. You can also skip this guide completely and simply go to the documentation page for Java in Visual Studio Code. But check this guide below for a laser-focused walkthrough in getting the most out of VS Code for Java development.

Initial Setup

There is a set of extensions that are the minimum requirement to get core Java support in VS Code. Let's dig into each of them.

Java Extension Pack by Microsoft  

This extension pack bundles the five extensions below explained in detail, and it will help you get started without digging too much.

Language Support for Java by Red Hat

This is the one and the only extension you are required to install to have Java support. All other extensions are complementary but do consider them based on the types of projects you'll be working on.

Once you install language support, you can read and edit Java source code. Start by creating a HelloWorld.java file and open on VS Code (or create the file within and then save in some folder).

Once done, you can open the terminal — on Mac OS, type ⌘+` — and then type javac HelloWorld.java to compile the file.

Embedded terminal on VS Code

This will generate a HelloWorld.class file. Run it with java HelloWorld.

The Language Support for Java extension brings Java support through the use of the Eclipse Language Server Protocol. Learn more about Eclipse LSP.

Other Features

This extension adds many other capabilities that will help you navigate, write, refactor, and read Java source code quickly enough that you may as well ditch an IDE in favor of a lightweight text editor.

Breadcrumbs navigation (top) — Outline view (bottom left)

Check the overview page for a complete list of its features and refactoring shortcuts.

Debugger for Java by Microsoft 

Once you get the basics of writing and reading Java code on Visual Studio Code, the next step is for sure running and debugging. This extension provides exactly that. It will use the default JAVA_HOME on your computer, but this can be customized of course.

Run | Debug Java applications

It has all the capabilities of a normal Java IDE debugging feature, and it allows greater customization and control over how things are executed and how the debugger is connected to the JVM. It works with remote JVMs as well.

Once this extension is installed, you will notice two hyperlinks above the main method, like in the picture above. You can click Run, and the code will be compiled and executed. You can also set a breakpoint and hit Debug.

For remote debugging, you will need to add a new configuration. Go to the Debug view (Shift+⌘+D on Mac) and hit the gear ⚙ config️ button. This will open the launch.jsonfile. Click on the Add configuration blue button you see on the screen. That should open a pop-up menu like the one below.

Now, you can customize a launch that can plug to a remote JVM. Just provide all the details like hostname and port number.

As any debugging IDE, you can scope variables, stack trace, and even make changes to variables content during execution time.

Changing Java variables during debugging on VS Code

And that completes the basic setup of Visual Studio Code for reading, writing, running, and debugging Java.

Intermediary Setup

Once you get used to the basics of Java coding, you will soon need to work with libraries, dependencies, classpath and so on. The best way to improve Java support on Visual Studio Code is by adding these three extensions:

  1. Java Dependency Viewer
  2. Maven for Java

Let's take a look at each of them.

Java Dependency Viewer by Microsoft 

This extension will give you two core capabilities. The main one is the concept of a "Project" that you can use to manually add libraries (JARs) to it. The second one is that it allows you to visualize the classpath your project is currently set for, even if it is a Maven project (see Maven for Java extension next).

Open the command pallet (Shift+⌘+P) and type create java:

Create a Java Project

This will ask you for a location where the project will be created. A project consists of a folder with the same name as the project name (it will be the next question from the command pallet once you select a location, for example, myworkspace).

Once the project is created, VS Code will open this new folder on a new window.

New Java Project

As you can see, the project has a basic structure with bin and src folders. Inside src, you will find a basic Java class to get started. If you are an experienced Java developer, you will quickly figure out that this extension utilizes the Eclipse project format, simply because it works well with the Eclipse Language Server Protocol and the other extensions.

Adding Libraries and JARs

You can add libraries by editing the .classpath file with custom entries of JARs that may be placed anywhere, for example, a lib folder. The extensions will automatically load those libraries in the classpath and you should be able to run your code.

Editing .classpath to add extra libraries.

Maven for Java by Microsoft

Maven is the most widely used project build and dependency management tool in the Java ecosystem. So, this extension finally gets you to a point where you can work on pretty much any Java project, of any kind, through Visual Studio Code.

You will be able to generate and bootstrap Maven projects through Maven archetypes (skeletons), manage dependencies and trigger Maven goals, and edit the pom.xml file with some smart code completion.

Maven commands

Let's take a look:

  • Open the command pallet again, and type Maven.
  • Select Generate from Maven Archetype.
  • Select maven-archetype-quickstart.

The extension will ask for a destination folder, where the project folder will be generated. It will bring you to the terminal where you will have to input parameters to the Maven command-line, but don't worry as it will be guided.

Once the project is created, right from the terminal, call code <folder name>.

Open the newly created folder on VS Code

Alright, you should have your Maven project now opened on VS Code. The very basic thing you can do is run your code. You have two choices here:

  1. Run with the Run hyperlink inside the App class next to the main method, as we learned earlier.
  2. Use Maven, of course.

If you use the Debugger for Java extension (Run | Debug) triggers, the extension will use the classpath generated by Maven, to ensure all dependencies are properly added to the classpath.

To run with Maven though, you can either use the Terminal, as usual, or open the Command Pallet and type Maven Execute Commands.

Command Pallet showing Maven actions

It will ask you to select a project. Since you have only one, hit Enter on it. Next, what you will see is a list of all the default core Maven goals. Hit package to generate the JAR file.

If you want to run custom goals, such as those inherited from Maven plugins, you can use the Maven view:

Execute goals from Maven plugins

Once you edit your pom.xml file to add dependencies, VS Code will automatically reload the classpath, and you should be able to import classes and packages from your new dependencies. Clean and smooth.

Java Test Runner by Microsoft 

The final step is to enhance how you run, debug, and visualize the results of your unit tests. This extension adds hyperlinks to unit tests (supports JUnit and TestNG) that can be executed individually, and you can see reports right away inside Visual Studio Code, like in the example below.

Running unit tests in VS Code

This extension will also enable the Test Explorer view, so you can focus on the unit tests of your code and write software in a more TDD style.

Text Explorer

This extension currently works only with Maven projects, so make sure you have the Maven for Java extension installed as well.

Advanced Setup

If you are now feeling comfortable with using VS Code for Java, it's time to ramp up your usage. Below is a list of some extensions that will enhance your experience on your day-to-day job.

This is just a hint on which extensions to add next, and not a de facto list, but can get you far ahead.


Hopefully, you are using Git already, whether through GitHub or any other service or environment. This extension will give you insights on authorship right into the source code, such as 'who added this method and when'.

This is how a Java class will look like once you have GitLens installed. Observe the lines that do not have numbers. Those are notes on commit history. You can also simply hover the mouse on a specific line and it will show when that was added, by whom, and on which commit hash.

REST Client 

If you are a developer building REST APIs, this is a must-have extension on your Visual Studio Code environment. With it, you will be able to edit .http files that contain HTTP calls. The editor will provide quick code snippets and templates, and it gives you magic hyperlinks within that once clicked, it triggers the HTTP call and opens the result right next to it. Here's a quick look.

REST Client

This is it! You now have a Visual Studio Code setup that is complete enough to any sort of real-world Java development.

Let me know if you have further questions or ideas for a future post on VS Code for Java.

Comment down below or hit me on Twitter: @brunoborges.

Bonus: Pivotal's Spring Setup and Gradle

To wrap this up, if you are an avid Spring developer, you will surely like to know that there are key extensions provided by Pivotal and Microsoft that will give you enhanced development experience for Spring Boot applications.

And finally, there is one Gradle extension that helps you write build.gradle files.

Here are some extra Spring tools for additional learning:

That's it. Thanks for reading!

debugger, gradle, ide, java, maven, visual studio code, vs, vs code

Published at DZone with permission of Bruno Borges . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}