I am setting up my java IDE with neovim, and I am searching for a good formatter. I have came across google-java-format, yet I want to know if there is any other alternative?
Videos
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
Extensionswindow 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 Clickand chooseFormat Document With..- In the command pallette that opens up on top, select
Language Support for Java(TM) by Red Hatoption
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 Hatis 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",
},
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.
JIndent (Commercial) does what you want:
http://www.jindent.com/
I've also used Jalopy in the past to do this, it's open source:
http://jalopy.sourceforge.net/
EDIT: I will note that Jalopy is fairly old and there doesn't seem to be any recent updates to it. I'm not quite sure if it will work with Java 5/6. You might want to look at Jacobe instead:
http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html
There's also a more complete list of formatters here on Roedy Green's site:
http://mindprod.com/jgloss/beautifier.html
You can also run Eclipse reformatter from command line.
Here is the Eclipse documentation of the feature
First you have to create configuration file specific to your project of your Java code formatter settings.
Then save {projectDirectory}/.settings/org.eclipse.jdt.core.prefs to another location.
Now you will call the formatter.
"c:\path\to\eclipse\eclipse.exe" -vm "C:\path\to\jdk\bin\java.exe" -application org.eclipse.jdt.core.JavaCodeFormatter -config "c:\path\to\org.eclipse.jdt.core.prefs" "C:\path\to\your\src"
[WARNING] This seems to have stopped working in Eclipse 2018. Use Eclipse Oxygen.