Setup VS Code for Java development

To setup VS Code for Java development, you need to install some extensions to provide core language features such as compilation and execution of Java programs, debugging, syntax highlighting, formatting, and code completion. Also you need a Java Development Kit (JDK) installed. If you do not know what a JDK is, don’t worry we will get to it!

It should be quick and easy to get up and running, but is it?

The VS Code docs has a Java guide that covers things quite well. However, I will go through how to setup things up from zero, with some beginner-friendly explanation.

Also, I will see how VS Code fares with Spring Boot applications, and see if you can benefit from using some extensions for this.

Step 1 - Install a Java Development Kit (JDK)

To use Java within Visual Studio Code, you need to install a Java Development Kit (JDK) on your computer. A JDK is the development environment used for compiling and executing Java applications.

You can check if you have a JDK is installed by running the command java -version on the command-line. You should see something like this if you have a JDK installed:

terminal

java -version
openjdk version “17.0.6” 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)

Since the JDK supplies the compiler for your Java programs, the JDK you install determines what Java version you are able to code in. For example, if you want to use the newer functional programming features found in Java 8, then you need to install at least the Java 8 JDK.

Which version of the JDK should I install?

The short answer: I recommend that you go with at least JDK version 17 by Adoptium Eclipse Temurin.

If you have experience with JDKs before, you can jump to step 2. Otherwise read on to know why I have made this recommendation and understand what you are doing exactly.

Java versions are released more frequently now than in the past:

For local development, you can install whatever version you want! However, nowadays Java 8 is usually the absolute minimum version used and it is advisable to install nothing less than that.

Businesses tend to stick with the latest LTS version for security reasons. Version 17 is the current LTS version.

Since the release of version 1.8 of the Language Support for Java by Red Hat extension (June 30th, 2022), you need to install at least JDK 17 to be compatible.

Which JDK distribution should I use?

The other (slightly annoying) thing with JDKs now is that many different JDK distributions exist. Some companies charge for their JDK and there are some funny terms and conditions associated with them. The various distributions are discussed here.

The summary is that the recommendation is to install Adoptium Eclipse Temurin’s JDK because it is run by a reputable open source consortium.

Crazy name, right? Sounds like a transformer! 🤖

To install this JDK, you can follow Adoptium’s installation guide. It covers every operating system.

To be set-up correctly, usually you must add the location of the installed JDK to the system path variable for your OS. This ensures that the java command is available on your command-line and system wide.

This is why I prefer to use a bash utility called SDKMan to do everything in one step. I can run the command sdk install java and it will install the latest JDK on my system.

Step 2 - Install the VS Code extensions

Working with Java, you will need extensions for:

  1. General language support including compilation, execution, syntax highlighting, and code completion.
  2. Debug and testing code (debugger).

Optionally, you can install extensions for:

  1. Pretty formatting your code (formatter).
  2. A test runner for executing your test suites.
  3. AI-assisted code completion.
  4. Working with a build tool such as Maven or Gradle.
  5. If you work with a framework like Spring Boot, you may like to have some assistance there also.

Microsoft provides a Extension Pack for Java extension that bundles the most commonly required extension for getting up and running with Java development. It includes the following extensions:

  1. 📦 Language Support for Java™ by Red Hat
    • Code navigation
    • Autocompletion
    • Error reporting
    • Code formatting (formatter)
    • Refactoring
    • Annotation processing support (automatic for Maven projects)
    • Basic Gradle support (Android not supported)
    • Code Snippets
  2. 📦 Debugger for Java
    • Debug Java code
  3. 📦 Test Runner for Java
    • Run & Debug JUnit/TestNG Test Cases
  4. 📦 Maven for Java
    • Support to scaffold maven projects.
    • Provide shortcuts to common goals, plugin goals, and customized commands.
    • Add Maven Projects panel to Explorer view
  5. 📦 Project Manager for Java
    • Create projects through a wizard (series of questions in Command Palette).
    • Adds Java Projects panel to Explorer view to manage project.
  6. 📦 Visual Studio IntelliCode
    • AI-assisted code completion for Java (and Python, and TypeScript/JavaScript).

VS Code needs to know where the JDK is, it searches in the following order until a JDK that meets its minimum requirement is found:

You can explicitly set the JDK location through the java.jdt.ls.java.home setting if you want to.

I installed the extension pack and disabled the Visual Studio IntelliCode extension, as I did not want to use it! You can install them individually if you prefer.

If you use Gradle instead of Maven, you can disable the Maven for Java extension and install the Gradle for Java extension.

You can try to run a “Hello World” program to see if you everything is set-up correctly. Here is an example for your convenience,:

Java
// HelloWorld.java
public class HelloWorld {
public static void main(String []args) {
System.out.println("Hello World"); // prints Hello World
}
}

Open this file and press F5 (or right click in the editor and select Run Java from the context menu) to run it.

running hello world class in VS Code

You will see “Hello World” output to the Terminal panel.

With these extensions installed, you have now have a lot of Java-related commands available that you can find them by searching for “Java” in the command palette.

java commands in the command palette

You may want to familiarize yourself with some of these commands for some regular tasks such as:

There is a Testing view in the sidebar as below. All your JUnit tests should be detected and shown here.

testing view in the sidebar

And now you have the Java Projects and Maven panels in the Explorer view in the sidebar as below.

java project and maven panels in the explorer view

Working with Spring Boot

First I wanted to try out running a Spring Boot app, so I opened a Spring project I created for a beginners tutorial. The maven dependencies should be auto-imported when you open the project. You will see a “Open Java Projects” progress dialog at first.

I opened up the Java file with the main method (UserApplication.java) and hit F5 to run it.

run spring boot app

And it worked first time! I was pleasantly surprised.

To see if tests were working as expected, I switched to the “with-tests” branch that has the tests. I opened the test suite for the project (src/test/java/UserTestSuite.java) and hit F5 to run it. The tests are executed and are shown in the Testing view in the sidebar as below.

running tests for spring boot app

Not too shabby! You can run and test a Spring Boot app the same way as any other Java app really.

There are some things we can add to make working with Spring Boot more pleasant. For example, .properties files do not have any syntax highlighting or autocompletion, that would be a nice addition. Again, you don’t need to think too hard on this one, you can install the Spring Boot Extension Pack that includes the following extensions:

  1. 📦 Spring Boot Tools
    • Provides language support for working with Spring Boot application.properties, application.yml and .java files.
  2. 📦 Spring Initializr
    • Quickly generate a Spring Boot project with a wizard.
  3. 📦 Spring Boot Dashboard
    • Provides an explorer in the side bar to view and manage all available Spring Boot projects in your workspace
    • Debugging support for a Spring Boot app.

I did notice that Spring Boot Tools is sloww to load. It takes 843ms on my machine! 🐌

I will evaluate these further to see if they are worth keeping or not!

Conclusion

I found it fairly smooth to get up and running with Java with VS Code. I will need to spend more time working on Java projects in VS Code to give a true assessment. A couple of the extensions I may not use.

Time will tell if VS Code will be my code editor of choice for Java. I still instinctively use NetBrains IntelliJ IDEA. Maybe, I will write a follow-up later to compare the experience of using different editors and IDEs for Java.

Until next time! 👋

Tagged