I spent some time working on this same problem yesterday. The best and most reliable solution I was able to come up with was to install google-java-format into my path and then use the RunOnSave extension to invoke the binary for files with the .java extension.

Install emeraldwalk/vscode-runonsave:

$ code --install-extension emeraldwalk.RunOnSave

Install google-java-format for your system:

# Example using Homebrew on OS X
$ brew install google-java-format

Configure RunOnSave in Visual Studio Code settings:

{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.java$",
        "cmd": "google-java-format --replace ${file}"
      },
    ],
  },
}

I describe more of the methodology in this blog post.

Answer from sethvargo on Stack Overflow
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Google Java Format for VS Code - Visual Studio Marketplace
Extension for Visual Studio Code - Visual Studio Code extension to format Java files using google-java-format program to comply with Google Java Style.
Top answer
1 of 7
12

I spent some time working on this same problem yesterday. The best and most reliable solution I was able to come up with was to install google-java-format into my path and then use the RunOnSave extension to invoke the binary for files with the .java extension.

Install emeraldwalk/vscode-runonsave:

$ code --install-extension emeraldwalk.RunOnSave

Install google-java-format for your system:

# Example using Homebrew on OS X
$ brew install google-java-format

Configure RunOnSave in Visual Studio Code settings:

{
  "emeraldwalk.runonsave": {
    "commands": [
      {
        "match": "\\.java$",
        "cmd": "google-java-format --replace ${file}"
      },
    ],
  },
}

I describe more of the methodology in this blog post.

2 of 7
2

google-java-format requires Java 16.0.1 SDK. You have to install it first.

There is an extension ilkka.google-java-format on the Visual Studio Code marketplace, but it is incompatible with current version.

I have to change it this way:

  1. Open file : C:\Documents and Settings\<USERNAME>\.vscode\extensions\ilkka.google-java-format-1.0.2\out\extension.js

  2. Update function provideDocumentRangeFormattingEdits this way:

    return new Promise((resolve, reject) => {
        let stdout = "";
        let stderr = "";
        let child = cp.spawn("java.exe",
            [
                "--add-exports", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
                "--add-exports", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
                "--add-exports", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
                "--add-exports", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
                "--add-exports", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
                "-jar",
                executablePath,
                "--lines",
                `${range.start.line}:${range.end.line}`,
                "-",
            ]);
    
  3. In Settings -> Preferences set executablePath to path to jar archive (google-java-format-1.10.0-all-deps.jar)

Enjoy

Discussions

Google Java Format
Is there a way to setup formatting with Google Java Format? Or an entirely new extension is needed which will run the jar formatter on opened file? If so, what would be the best way to implement th... More on github.com
🌐 github.com
10
January 24, 2018
vscode Google Java style

Why are so many People trying to use vsc for Java. Please dont. You wont be happy with it. Use eclipse or intellij.

More on reddit.com
🌐 r/vscode
16
8
February 12, 2021
vs code - Import google-java-format from Eclipse to VSCode - Stack Overflow
I have an archive plugin for Eclipse that contains an personalized google-java-format implementation, I want to import this to VSCode editor, is it possible to this?I found how to do this with xml ... More on stackoverflow.com
🌐 stackoverflow.com
Feature Request: Reformat all the code with Google Java Format tool on Save
It would be great to be able to run Google Java Format Tool on Save in VS Code to force consistent code style in each file of the project. This can be achieved in Eclipse by dropping in Google Java Format Eclipse Plugin, select it as a F... More on github.com
🌐 github.com
3
February 23, 2018
🌐
Seth Vargo
sethvargo.com › using-google-java-format-with-vs-code
Using google-java-format with VS Code | Seth Vargo
January 12, 2020 - This post explores how to format Java source files using google-java-format in Visual Studio Code.
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
google-java-format - Visual Studio Marketplace
Extension for Visual Studio Code - Yet another simple google-java-format runner
🌐
Visual Studio Code
code.visualstudio.com › docs › java › java-linting
Java formatting and linting
November 3, 2021 - You can open the editor with the command Java: Open Java Formatter Settings with Preview. In the editor, you can change the formatter settings and preview the effects. After saving the current editor, the changes will be saved to the formatter ...
🌐
GitHub
github.com › ilkka › vscode-google-java-format
GitHub - ilkka/vscode-google-java-format: Yet another google-java-format VS Code extension
This is yet another VS Code extension for running the google-java-format tool as a Java formatter. I could not make any of the others work for me, and I did not want to install an extension that will just run commands from random repositories on save. So, I wrote this, a super simple formatter provider that will just run the google-java-format script from e.g.
Starred by 3 users
Forked by 11 users
Languages: TypeScript 100.0% | TypeScript 100.0%
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 419
Google Java Format · Issue #419 · redhat-developer/vscode-java
January 24, 2018 - Is there a way to setup formatting with Google Java Format? Or an entirely new extension is needed which will run the jar formatter on opened file? If so, what would be the best way to implement th...
Author: redhat-developer
Find elsewhere
🌐
Visual Studio Marketplace
marketplace.visualstudio.com › items
Google Java Formatter - Visual Studio Marketplace
Extension for Visual Studio Code - Reformats Java source code to comply with Google Java Style for VS Code
🌐
DEV Community
dev.to › marcushellberg › how-to-configure-vs-code-java-formatting-1p10
How to configure VS Code Java formatting - DEV Community
June 30, 2021 - You can either use a formatter like the Google Java Style Guide (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.
🌐
ryo8000
ryo8000.com › set-up-a-java-formatter-in-visual-studio-code
Set Up a Java Formatter in Visual Studio Code | ryo8000
January 7, 2025 - Right-click within the file and select “Format Document.” The code in the Java file will then be formatted according to the Google Java Style. In the following section, I will explain how to change the formatter settings and adjust VSCode ...
🌐
Open VSX Registry
open-vsx.org › extension › josevseb › google-java-format-for-vs-code
Google Java Format for VS Code – Open VSX Registry
Visual Studio Code extension to format Java files using google-java-format program to comply with Google Java Style.
Author: ericpai
🌐
Reddit
reddit.com › r/vscode › vscode google java style
r/vscode on Reddit: vscode Google Java style
February 12, 2021 -

I am trying out vsc for Java development. I would like vsc to create new file templates in the Google style. Is there a way to configure vsc to do this? Or do I have to let it do its thing and then format it afterward?

Thanks.

Top answer
1 of 2
1

Could you get the xml profile in your mentioned plugin? If yes, make sure you have installed Java extension first, VSCode can use a specified Eclipse xml profile as it's formatter profile.

You can check the setting java.format.settings.url. When it's specified, the given formatter profile will be used in VSCode Java formatting.

Another way to use it is to trigger command Java: Open Java formatter settings, and it will help you generate a default formatter profile, you can change the content to what you want.

2 of 2
0

To directly set "Google Java Style" as "Java's format" on VSCode, please:

1.Install "Extension Pack for Java".

e.g: "Extension Pack for Java v0.9.1" + "Language Support for Java(TM) by Red Hat v0.64.1" for "Java1.8".

Making sure COMPILING, RUNNING and DEBUGGING are successful before "Next Step"!

2.Set the default formatter of Java into "RedHat".

2.1Using hotKeyMap to open the Settings of VSCode, e.g: Ctrl/Cmd + ,

( or Click the logo of VSCode "Code" button and choose "Preference" menu and then click "Setting")

2.2Type "javahome" in the "Search settings" space, then click "Edit in settings.json",

2.3Adding the below code into your "settings.json",

"java.format.settings.url": "https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml",
"java.format.settings.profile": "GoogleStyle",
"java.format.settings.imports.onDemand": false,
"java.format.settings.imports.regexp": "^[a-z.]*",
"[java]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "redhat.java",
    "editor.tabSize": 2
}

2.4Then restart the "VSCode", Using shift + alt/option + f to automatic "Java's format" into "Google Java Style".

END!

🌐
GitHub
github.com › JoseVSeb › google-java-format-for-vs-code
GitHub - JoseVSeb/google-java-format-for-vs-code · GitHub
Format your java files using Google Java Format program which follows Google Java Style (or AOSP).
Starred by 20 users
Forked by 5 users
Languages: TypeScript 93.8% | JavaScript 6.2%
🌐
GitLab
gitlab.com › mngrm3a › vscode google java format
mngrm3a / VSCode Google Java Format · GitLab
June 23, 2021 - Skip to content · VSCode Google Java Format · Project information · README · Created on · June 10, 2020 · Loading
🌐
CodingTechRoom
codingtechroom.com › question › -google-java-style-guide-vscode
How to Implement Google Java Style Guide Formatting in Visual Studio Code - CodingTechRoom
Solution: Ensure the 'Checkstyle for Java' extension is installed from the Marketplace. Mistake: Ignoring format settings in workspace or user settings. Solution: Check both user and workspace settings to apply necessary formatting configurations. Mistake: Forgetting to enable auto-format on save. Solution: Add "editor.formatOnSave": true to VSCode settings for automatic formatting.
🌐
GitHub
github.com › redhat-developer › vscode-java › issues › 450
Feature Request: Reformat all the code with Google Java Format tool on Save · Issue #450 · redhat-developer/vscode-java
February 23, 2018 - It would be great to be able to run Google Java Format Tool on Save in VS Code to force consistent code style in each file of the project. This can be achieved in Eclipse by dropping in Google Java Format Eclipse Plugin, select it as a F...
Author: redhat-developer