DEV Community

Cover image for Setting up VS Code for Java development
Rob OLeary
Rob OLeary

Posted on • Edited on • Originally published at roboleary.net

Setting up VS Code for Java development

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 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) 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)


Enter fullscreen mode Exit fullscreen mode

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 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.

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

Alternatively, you can install the Extension Pack for Java extension (as we will discuss next), and run the command "Java: Install New JDK". It just helps you download the installation files it seems.

install JDK in VS Code using a command

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 or Gradle (unless you are command-line junkie).
  4. If you work with a framework like Spring Boot, you may like to have some assistance there also.

Microsoft has a Extension Pack for Java extension that bundles the following 6 extensions:

  1. 📦 Language Support for Java™ by Red Hat
    • 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
    • Debugging 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 wizard.
    • Adds Java Projects panel to Explorer view to manage project.
  6. 📦 Visual Studio IntelliCode
    • 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 extension and install the Gradle for Java 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, :



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


Enter fullscreen mode Exit fullscreen mode

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

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:

  • 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.

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 dont 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

    • Language Server providing 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 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. 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, and share this article with others on social media. 💌

You can show your appreciation by buying me a coffee on ko-fi. 🙂

Top comments (2)

Collapse
 
c_b_d profile image
Carlota Dias

Thank you for the article ! I look forward to updates !

Collapse
 
robole profile image
Rob OLeary

vscode dancing gif