I have tried some proposals on here and here, but none of them works. How do you accomplish it for Java code or is there a way for general? En passant, I've install XML formatter in order that it may work, but not.
3 Answers
Formatting a JAVA Document in VSCode requires one to follow the below steps:
Install the plugin: Language Support for Java(TM) by Red Hat from the
Extensions
window in VSCode.- You can open the Extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X
Post Installation, from the required page with JAVA code
Right Click
and chooseFormat Document With..
- In the command pallette that opens up on top, select
Language Support for Java(TM) by Red Hat
option
The Java Document should be now properly formatted.
PS: Trying Alt+Shift+F may not work for users who have set their default formatter as Prettier - Code formatter or alike. By default, java formatting is not supported by it.
IfLanguage Support for Java(TM) by Red Hat
is the only formatter plugin installed, then Alt+Shift+F might work for you.
If you wish to bind Alt+Shift+F to format your JAVA Document,
then you can add the below configuration to your settings.json
by:
- Pressing Ctrl+, to open your
Settings
- From Top Right Corner, select
Open Settings(JSON)
either for User or Workspace settings. - Append the below JSON property to it and save:
"[java]": {
"editor.defaultFormatter": "redhat.java",
},

- 2,218
- 20
- 26
-
any option for eclipse theia or open vscode? – Banee Ishaque K Jul 31 '21 at 11:07
-
2The RedHat java plugin is an absolute joke on VS Code. It's incredibly slow, it shows errors that don't exist, it's completely unusable with large projects, it's always running out of memory and/or crashing, it never works right with submodules, it often doesn't pick up generated sources, it often mysteriously puts generated sources in the wrong folder, I can go on and on. It was an incredible waste of time trying to get it to work. To anyone reading this: Do yourselves a favor and instead use the NetBeans java plugin. Even though it's technically an alpha, It's far better in every respect. – Alvin Thompson Feb 11 '22 at 17:03
-
1@AlvinThompson I installed the NetBeans plugin. This is what I found: 1) It won't run or debug individual junit tests without adding the surefire maven plugin where RedHat didn't require any extras. 2) For me, it was vastly slower than RedHat. 3) When editing a single java file it has no context, e.g., auto-add imports, where RedHat works (I edit single files to use with jshell). 4) Most importantly, like RedHat, it wouldn't load a custom settings formatter file. I could see it giving errors, but didn't bother investigating further given all the other issues. – dudeNumber4 Aug 24 '22 at 00:16
For proper formatting, you need to install a VSCode extension that provides Java formatting. Try this one from Redhat: https://marketplace.visualstudio.com/items?itemName=redhat.java
After the extension is installed, that standard code format commands / shotcuts should work for Java code.

- 58,117
- 21
- 175
- 206
-
You can also set a formatter configuration file as it is shown here https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings – Sangimed Jan 23 '19 at 19:48
-
Before installing this extension, I had to install OpenJDK: https://jdk.java.net/archive/ – Tonatio Jun 09 '19 at 14:20
-
3The problem with this one is that it's insane bloatware if all you want is source formatting. – Mike 'Pomax' Kamermans Mar 05 '21 at 21:32
-
@Mike'Pomax'Kamermans which source formatter is without bloatware? Maybe the solution further down with eclipse-java-google-style? – Timo Apr 30 '23 at 19:25
-
This doesn't help a very new person when you don't say what the "standard format commands / shortcuts" are – Lee Meador Aug 23 '23 at 19:16
I feel it's a good idea to store a configuration with each project (and not globally).
- open workspace settings json file. Following steps work on my laptop -
- press
F1
- select
Preferences: Open Workspace Settings (JSON)
- press
- paste the below there -
{
"java.format.settings.url": "./eclipse-java-google-style.xml"
}
- Now you can create a file in the root folder
eclipse-java-google-style.xml
which has the settings. - There, you can paste the contents of eclipse-java-google-style.xml. Optionally set
org.eclipse.jdt.core.formatter.join_wrapped_lines
to false.

- 780
- 11
- 14