Stand-alone Java code formatter/beautifier/pretty printer?

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


Jalopy may be of interest. It will plug into Ant and thus is independent of your IDE.

Astyle may also be useful. It's open source (like Jalopy) but written in C++. It's geared towards formatting Java/C/C++ and can be batch-driven (via shell scripts or similar)


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.