Visual Studio Code for the Java developer

Visual Studio Code (VS Code) is a lightweight but extensible code editor. It is great for editing any kind of text files like helm templates or Kubernetes deployment descriptors, Azure Resource Manager (ARM) templates, Dockerfiles, CI/CD pipelines, or markdown-based documentation.

It is a different animal than a full-fledged IDE like Eclipse or Idea and mot meant to be a full replacement. IDEs are still the tools of choice for large application development that has complex refactoring and code generation workflow capabilities.

Visual Studio Code is a streamlined code editor with support for development operations like debugging, task running, and version control. It aims to provide just the tools a developer needs for a quick code-build-debug cycle and leaves more complex workflows to fuller featured IDEs

However, there are cases where we are constantly switching between, scripts, templates, docs, and code. In such cases it does make sense to stay in the same tool. Also, more applications are polyglot these days and using the same editor for all of them can result in higher productivity overall.

These are scenarios where we can leverage VS Code for viewing Java code files or run a quick debug or test session here and there and even make changes to a certain degree. That is especially useful for the non-hardcore Java developer that wants to leverage the amazing in-container development capabilities of VS Code without the need of having a complete Java environment installed. This goes up to leveraging cloud centric development like GitHub Codespaces or Eclipse Theia.

Finally, some got frustrated in recent years on how slow and clumsy overburdened IDEs have become and are looking for a lightweight alternative.

There is a rich Java ecosystem around VS Code now that companies like Microsoft, Redhat and Picotal have invested in. Time for the Java developer to leverage it. Let's get started!

Java VS Code setup with Dev Containers

As mentioned, I always run VS Code using its remote containers' capability. That means that the entire development environment including all the required OS level tools (e.g., Git, JDK, Maven) run in a Docker container. The required VS Code plugins are configured for the environment as well.

First, we create devcontainer.json file in our repository. The best way to do so is to copy it from the microsoft/vscode-dev-container project and extend or adapt where necessary. To create one from scratch is possible as well.

I have taken the Java template, cleaned out the node parts and extended it with:

  • Code formatting and Checkstyle based on the Google Java Style Guide
  • Extensions for Java, Docker, Markdown, Azure Pipelines, Checkstyle, Spring Boot and Lombok
  • Open the port 8080 to I can access apps from my local web client.

This is my default setup as I have all these components at minimum in my Java projects.

 1{
 2  "name": "Java",
 3  "build": {
 4    "dockerfile": "Dockerfile",
 5    "args": {
 6      "VARIANT": "11"
 7    }
 8  },
 9  "settings": {
10    "terminal.integrated.shell.linux": "/bin/bash",
11    "java.home": "/docker-java-home",
12    "java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
13    "java.format.settings.profile": "GoogleStyle",
14    "maven.executable.path": "/usr/local/sdkman/candidates/maven/current/bin/mvn",
15    "java.checkstyle.configuration": "/google_checks.xml"
16  },
17  "extensions": [
18    "vscjava.vscode-java-pack",
19    "DavidAnson.vscode-markdownlint",
20    "ms-azuretools.vscode-docker",
21    "ms-azure-devops.azure-pipelines",
22    "shengchen.vscode-checkstyle",
23    "pivotal.vscode-spring-boot",
24    "vscjava.vscode-spring-boot-dashboard",
25    "GabrielBB.vscode-lombok"
26  ],
27  "forwardPorts": [
28    8080
29  ],
30  "remoteUser": "vscode"
31}
32

Next to devcontainer.json we either need a Docker image somewhere in the cloud that has our OS environment ready (e.g., Java SDK, maven etc.) or a Dockerfile doing so. I use the later which is again copied from the template and cleaned up a bit. It would allow to add additional OS level tooling in an easy manner.

 1# [Choice] Java version: 8, 11, 15
 2ARG VARIANT=11
 3FROM mcr.microsoft.com/vscode/devcontainers/java:${VARIANT}
 4
 5# Install Maven
 6ARG MAVEN_VERSION=""
 7RUN su vscode -c "source /usr/local/sdkman/bin/sdkman-init.sh && sdk install maven \"${MAVEN_VERSION}\""
 8
 9# [Optional] Uncomment this section to install additional OS packages.
10# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
11#     && apt-get -y install --no-install-recommends <your-package-list-here>

These two files in a .devcontainer directory in my projects repository means that if I open the repository folder VS Code will ask me "Reopen in container?" and I am good to go. No more painful installation and configuration marathons.

It will take a moment when opening the first time as the image needs to be built. Obviously, Docker daemon needs to run on my machine.

Starting the Java Dev Container

Rund and debug a Java Application

As my demo is a Spring Boot application, we can simply click start in the Spring-Boot-Dashboard menu entry and call the controller http://localhost:8080/hello.

Run the Spring Boot application

As we have opened the port in the devcontainer.json we can call from the Host machine as well.

Call the Spring Boot application

VS Code supports debugging as mentioned. For this case, the app can be started in debug as well and breakpoints are hit.

Debug the Spring Boot application

Run Java tests

Tests can be run in VS Code's test tab, from the test report if opened or directly from within (test) code. The later comes in quite handy especially for debugging purposes.

Test the Spring Boot application

Next steps

This was a very brief introduction into the topic but with these simple steps a lot can already get done in many projects.

There are many more features that are worth exploring, e.g.:

So far, so good. Have fun with VS Code!