Setting up VS Code for Java development
Tags: java, vscode, tooling, beginners
Published: 2022-03-17 11:35:24

Visual Studio Code (VS Code) is oriented towards front-end development. To use VS Code with Java, you need to install some extensions to provide core language features such as compilation and execution of Java programs, debugging, syntax highlighting, and code completion. It *should* be quick and easy to get up and running, but is it?

The VS Code docs has a [Java guide](https://code.visualstudio.com/docs/java/java-tutorial) that covers everything from top to bottom. I will go through how the set-up process went for me with some beginner-friendly explanations, and see how VS Code fares with Spring Boot applications.

## Installing a Java Development Kit (JDK)

To use Java within Visual Studio Code, you need to install a [Java Development Kit (JDK)](https://www.infoworld.com/article/3296360/what-is-the-jdk-introduction-to-the-java-development-kit.html) 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:

```
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2.20.04, 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](https://www.javaworld.com/article/3314640/functional-programming-for-java-developers-part-1.html) found in Java 8, then you need to install at least the Java 8 JDK.

### Which version should I use?

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

- A new major version number is released every six months, in March and September. Additionally, there are quarterly bug fix updates.

- Every three years, the September release will be a Long-Term-Support (LTS) release, which gets updates for at least three years.

For local development, you can install whatever version you want! Nowadays, Java 8 is usually the 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. 17 is the current LTS version.

So, choose a JDK between version 8 and 17 to be compatible with VS Code.

> Update: The release of v1.8 (June 30th, 2022) of Language Support for Java by Red Hat extension now requires at least JDK 17.

### 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](http://whichjdk.com/#distributions).

In summary, [it is recommended that you install Adoptium Eclipse Temurins distribution](https://adoptium.net/). This distribution is managed by a reputable open source consortium. To install this distribution, you can follow [the installation guide](https://adoptium.net/installation.html) to get set-up for your particular platform/OS.

Alternatively, you can install the [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack) extension (as we will discuss next), and run the command "Java: Install New JDK". It just helps you download the installation files it seems.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6enli30qhwmepkkpf6pm.png" alt="install JDK in VS Code using a command" width="910" height="550" loading="lazy"/>

To be set-up correctly, usually you must add the location of the installed JDK to the PATH environment variable for your OS.

## Install *Extension Pack for Java* extension

Working with Java, you will need extensions for:

1. General language support (compilation, execution, syntax highlighting, code completion).
2. Debugger/test runner support.
3. Support for a build tool such as [Maven](https://maven.apache.org/) or [Gradle](https://gradle.org/) (unless you are command-line junkie).
4. If you work with a framework like [Spring Boot](https://spring.io/), you may like to have some assistance there also.

Microsoft has a [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack) extension that bundles the following 6 extensions:

1. 📦 [Language Support for Java™ by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java)
   - Code Navigation
   - Autocompletion
   - Code formatting
   - Refactoring
   - Annotation processing support (automatic for Maven projects)
   - Basic Gradle Java project support (Android not supported)
   - Code Snippets
2. 📦 [Debugger for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-debug)
   - Debugging Java code
3. 📦 [Test Runner for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test)
   - Run & Debug JUnit/TestNG Test Cases
4. 📦 [Maven for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven)
   - 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](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-dependency)
   - Create projects through wizard.
   - Adds *Java Projects* panel to Explorer view to manage project.
6. 📦 [Visual Studio IntelliCode](https://marketplace.visualstudio.com/items?itemName=VisualStudioExptTeam.vscodeintellicode)
   - Provides AI-assisted completion for Python, TypeScript/JavaScript and Java developers.

VS Code needs to know where the JDK is, it searches in the following order until a JDK meets current minimum requirement (Java 1.5 or above is supported):
 - the `JDK_HOME` environment variable
 - the `JAVA_HOME` environment variable
 - on the current system path (`PATH` environment variable).

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](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven) extension and install the [Gradle for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-gradle) extension.

At this stage, 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 <kbd>F5</kbd> (or right click in the editor and select *Run Java* from the context menu) to run it.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crjrv82fqsc9ghj6c5in.png" width="954" height="959" loading="lazy"/>

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.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ebjxvd8au8c77eliijt.png" alt="java commands in the command palette" width="812" height="483" loading="lazy"/>

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

- Creating a new project via a wizard (`Java: Create Java Project...`)
- Create a new class (`Java: Create New Class`).

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

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mifyubbjbx0g820pple1.png" alt="testing view in the sidebar" width="346" height="359" loading="lazy"/>

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

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bpze6nx55sfqviu713or.png" alt="java project and maven panels in the explorer view" width="344" height="636" loading="lazy"/>

### 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](https://github.com/robole/spring-boot-api-for-beginners). 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 <kbd>F5</kbd> to run it.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30qsjkvrbsn102gmyrey.png" alt="run spring boot app" width="914" height="1008" loading="lazy"/>

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 <kbd>F5</kbd> to run it. The tests are executed and are shown in the *Testing* view in the sidebar as below.

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r40ag93kt1w3wri9jit5.png" alt="running tests for spring boot app" width="955" height="1020" loading="lazy"/>

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 dont need to think too hard on this one, you can install the [Spring Boot Extension Pack](https://marketplace.visualstudio.com/items?itemName=pivotal.vscode-boot-dev-pack) that includes the following extensions:

1. 📦 [Spring Boot Tools](https://marketplace.visualstudio.com/items?itemName=Pivotal.vscode-spring-boot)
 - Language Server providing support for working with Spring Boot `application.properties`, `application.yml` and `.java` files.

2. 📦 [Spring Initializr](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-spring-initializr)
 - Quickly generate a Spring Boot project with a wizard.

3. 📦 [Spring Boot Dashboard](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-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 slow to load. It takes 843ms on my machine! 🐌

I may not use these in the long-run, especially if the performance sucks!

## 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 few 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](https://www.jetbrains.com/idea/). Maybe, I will write a follow-up later to compare the experience of using different editors/IDEs for Java.

Until next time! 👋

---
**Thank you for reading! Feel free to subscribe to my [RSS feed](https://www.roboleary.net/feed.xml), and share this article with others on social media. 💌**

**You can show your appreciation by buying me a coffee on [ko-fi](https://ko-fi.com/roboleary). 🙂**