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 OverflowI 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.
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:
Open file :
C:\Documents and Settings\<USERNAME>\.vscode\extensions\ilkka.google-java-format-1.0.2\out\extension.jsUpdate 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}`, "-", ]);In Settings -> Preferences set executablePath to path to jar archive (google-java-format-1.10.0-all-deps.jar)
Enjoy
References to google-styleguide in formatter documentation
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.comjava - Google Style formatting - Stack Overflow
Google Java Format
How do I set up Google Java Style in IntelliJ/Eclipse/VSCode?
Can I customize Google Java Style?
What is the difference between Google Java Style and Oracle’s conventions?
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.
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.
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!
