To format Java in Visual Studio Code (VS Code), install the Language Support for Java(TM) extension by Red Hat (redhat.java), open a .java file and run Format Document: Shift+Alt+F on Windows, Ctrl+Shift+I on Linux, Shift+Option+F on macOS. To format on every save, turn on editor.formatOnSave. To use a style such as Google’s, point java.format.settings.url at an Eclipse formatter profile.
Table of Contents
Install the Java formatter in VS Code
The formatter is part of Language Support for Java(TM) by Red Hat, and VS Code’s Java documentation says that extension provides the formatting settings. The Extension Pack for Java (vscjava.vscode-java-pack) includes it, so either install works.
Format a Java file or a selection in VS Code
- Open a
.javafile; the extension’s README says it activates when you first access one. - Run Format Document from the Command Palette (Ctrl+Shift+P, Shift+Cmd+P on macOS) or with the shortcut above.
- To format part of a file, select it and run Format Selection: Ctrl+K Ctrl+F on Windows and Linux, Cmd+K Cmd+F on macOS.
No profile is needed: the Java documentation says the file is then formatted using default settings.
Format Java on save in VS Code
VS Code’s Basic editing page lists format on save as off by default. Turn it on for Java only with a language block in settings.json:
{
"[java]": {
"editor.formatOnSave": true
}
}
java is the language ID for Java. Use your user settings.json to cover every project, or .vscode/settings.json to cover one workspace.
Use Google style or your own formatter profile
The formatter reads an Eclipse formatter XML. For Google’s style, the Java documentation uses this pair of settings:
{
"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/eclipse-java-google-style.xml",
"java.format.settings.profile": "GoogleStyle"
}
java.format.settings.url takes a URL or a local file path. The profile name matters for XML files with several profiles; Google’s, which I fetched on 1 October 2026, has one, named GoogleStyle. The URL points at the gh-pages branch, which can change, so for a team I would commit a copy and use its path.
To write your own profile, you can start from a short file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="23">
<profile kind="CodeFormatterProfile" name="our-house-style" version="23">
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="space"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.size" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.indentation.size" value="2"/>
<setting id="org.eclipse.jdt.core.formatter.lineSplit" value="100"/>
</profile>
</profiles>
Those are four of the six settings in the run below. Save them as an XML file, point java.format.settings.url at it, and set java.format.settings.profile if the file holds several profiles. If the indentation ignores the profile, see the editor.tabSize row under “When Java formatting does nothing in VS Code”. The documentation does not say how a relative path or ${workspaceFolder} is resolved, and I did not test either. The README says the command Java: Open Java Formatter Settings creates a settings file if none exists.
Java formatter settings in VS Code
The extension declares five java.format settings. These defaults are from the package.json of the published 1.57.2026093008 release, read on 1 October 2026.
| Setting | Default | What it does |
|---|---|---|
java.format.enabled |
true |
Enables or disables the default Java formatter |
java.format.settings.url |
null |
URL or file path of an Eclipse formatter XML |
java.format.settings.profile |
null |
Profile name to use from that XML |
java.format.comments.enabled |
true |
Includes comments when formatting |
java.format.onType.enabled |
true |
Formats the block as you type ;, Enter or } |
When Java formatting does nothing in VS Code
| Check | Why it stops formatting | Fix |
|---|---|---|
| A Java formatter is installed and enabled | Without one, VS Code asks: “There is no formatter for ‘java’ files installed.” | Install or enable redhat.java |
editor.defaultFormatter |
It “takes precedence over all other formatter settings”. If it names an extension that cannot format Java, such as Prettier (its documentation does not list Java), VS Code says the extension “is configured as formatter but it cannot format” Java files | In the "[java]" block, set "editor.defaultFormatter": "redhat.java" |
java.format.enabled |
The default is true; false turns the default Java formatter off |
Set it to true |
editor.formatOnSave |
It is off by default, so saving formats nothing until you turn it on | Add the "[java]" block shown earlier |
| The JDK that starts the language server | The language server, which ships the JDT formatter, needs a tooling JDK. On the universal build or a platform without an embedded JRE, the README requires Java 25 or above | Install JDK 25 or later and set java.jdt.ls.java.home; unset, the extension searches JDK_HOME, JAVA_HOME, then the system path |
editor.tabSize and editor.insertSpaces |
VS Code sends its tab size and spaces-or-tabs choice with each format request and, per the language server’s source, they replace the profile’s tabulation.size and tabulation.char |
In the "[java]" block, set "editor.tabSize" and "editor.insertSpaces" to match the profile, and add "editor.detectIndentation": false, because Basic editing says the indentation VS Code detects in a file overrides them |
How the profile decides the output
The profile is an Eclipse formatter XML because the formatter is Eclipse’s. In August 2026 I unpacked the published redhat.java VSIX, version 1.56.2026080608. Its language server ships 114 plugin JARs, among them org.eclipse.jdt.core_3.47.0.v20260805-2213.jar, the same JDT core that Eclipse itself formats with. Its default profile, formatters/eclipse-formatter.xml, looks like this, abridged:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="23">
<profile kind="CodeFormatterProfile" name="Eclipse" version="23">
<setting id="org.eclipse.jdt.core.formatter.align_arrows_in_switch_on_columns" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.tabulation.char" value="tab"/>
...
400 <setting> elements in all. Every key is an org.eclipse.jdt.core.formatter.* option and every value is a string: a flat list, no nesting, no defaults section. I read the current release, 1.57.2026093008, on 1 October 2026: again 114 plugin JARs and a 400-setting profile, with JDT core 3.48.0. I did not re-run the formatter on it.
I did not run any of this in VS Code. Instead I pulled the 3.47.0 JDT core JAR out of that VSIX, called ToolFactory.createCodeFormatter(Map) with settings parsed from a profile XML, and ran it on OpenJDK 26.0.2. Starting from this:
public class Messy {
public int add( int a,int b ) {
if(a>b){return a+b;}
else { return b-a; }
}
}
The profile held six settings: the four shown earlier, plus brace_position_for_method_declaration set to next_line and insert_space_after_opening_paren_in_method_declaration set to do not insert. It produced:
public class Messy {
public int add(int a, int b)
{
if (a > b) {
return a + b;
} else {
return b - a;
}
}
}
The extension’s own bundled 400-setting profile, on the same input, produced tab indentation and a same-line brace. Two things follow. First, the profile really is what decides the output. Second, a partial profile is legal: settings you omit fall back to JDT defaults, so you do not need to export all 400 to change indentation. Six lines is a valid file.
I put the driver and both runs in a script you can rerun: /verify/format-code-in-vs-code.sh. It needs a JDK, python3 and unzip, downloads the published extension, and never launches an editor.
The shortcuts, settings and messages on this page come from VS Code’s documentation and source and the extension’s README and package.json, not from an editor session. I did not observe how the extension reloads a changed profile or formats on save, or whether the language server applies settings of its own on top of a profile.