How to configure VS Code Java formatting
Tags: java, vscode, tutorial
Published: 2021-06-30 22:12:56

---
title: How to configure VS Code Java formatting
published: true
description: Configure VS Code Java formatting to display chained methods nicely.
tags: java, vscode, tutorial
---

I've been using [VS Code for Java](https://marketplace.visualstudio.com/items?itemName=redhat.java) for the past year or so. For the most part, I've been super happy. But one thing has been driving me crazy.

## The problem

I write a nicely formatted chain of methods, like:

```java
public Weather getWeather(Double lat, Double lon) {
  return webClient.get()
      .uri(uri -> uri.path("/")
          .queryParam("lat", lat.toString())
          .queryParam("lon", lon.toString())
          .queryParam("units", "metric")
          .queryParam("apiKey", apiKey)
          .build())
      .retrieve()
      .toEntity(Weather.class)
      .block()
      .getBody();
}
```

But when I save the file, the formatter "helps" me out by smashing all of it on as few lines as possible:

```java
public Weather getWeather(Double lat, Double lon) {
  return webClient.get()
      .uri(uri -> uri.path("/").queryParam("lat", lat.toString()).queryParam("lon", lon.toString())
          .queryParam("units", "metric").queryParam("apiKey", apiKey).build())
      .retrieve().toEntity(Weather.class).block().getBody();
}
```

![A man punching a computer monitor](https://media.giphy.com/media/URcmhQ5PpztC0/giphy.gif)

## The solution

The solution ended up being simple, just not that well documented. VS Code Java uses a headless Eclipse not only for Intellisense and other code actions, but also for code formatting. 

A [helpful Gist](https://gist.github.com/samba2/7d2f244f4e0d2a6220ac99de570a5221) pointed to a way of using an Eclipse settings file to configure the formatter per project. But I wanted a solution that would work across projects. Finally, I found a super helpful [Stack Overflow answer](https://stackoverflow.com/a/63571908/5705554) that outlined how to do just that. 

### 1. Get a formatter config XML file

You can either use a formatter like the [Google Java Style Guide](https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml) (which I did) or export your Eclipse profile through `Preferences > Java > Code Style > Formatter`. 

Save the file somewhere convenient where it won't get deleted. I chose `~/.vscode/eclipse-java-google-style.xml`. 

### 2. Change the join wrapped lines property to false

In the settings XML file, find the `org.eclipse.jdt.core.formatter.join_wrapped_lines` property, and set its value to `false`. Add the property if you don't have it. 

```xml
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>

```

### 3. Tell VS Code to use the format config

Open the VS Code settings (`settings.json`) and add the following line (remember to update the path):

```json
"java.format.settings.url": "~/.vscode/eclipse-java-google-style.xml",
```

### 4. Profit

Restart VS Code. Change a Java file and the formatter should now produce a much nicer output.

![A boy giving a thumbs-up](https://media.giphy.com/media/XreQmk7ETCak0/giphy.gif)