Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stand-alone Java code formatter/beautifier/pretty printer? [closed]

I'm interested in learning about the available choices of high-quality, stand-alone source code formatters for Java.

The formatter must be stand-alone, that is, it must support a "batch" mode that is decoupled from any particular development environment. Ideally, it should be independent of any particular operating system as well. So, a built-in formatter for the IDE du jour is of little interest here (unless that IDE supports batch mode formatter invocation, perhaps from the command line). A formatter written in closed-source C/C++ that only runs on, say, Windows is not ideal, but is somewhat interesting.

To be clear, a "formatter" (or "beautifier") is not the same as a "style checker." A formatter accepts source code as input, applies styling rules, and produces styled source code that is semantically equivalent to the original source code. Syntactic modifications are limited to things like modifying code layout as in changing whitespace, or organizing import statements. Very little, if any, other refactoring is performed. A style checker also applies styling rules, but it simply reports rule violations without producing modified source code as output. So the picture looks like this:

Formatter (produces modified source code that conforms to styling rules)

Read Source Code → Apply Styling Rules → Write Styled Source Code

Style Checker (does not produce modified source code)

Read Source Code → Apply Styling Rules → Write Rule Violations

Further Clarifications

Solutions must be highly configurable. I want to be able to specify my own style, not simply select from a preset list.

Also, I'm not looking for a general purpose pretty-printer written in Java that can pretty-print many things. I want to style Java code.

I'm also not necessarily interested in a grand-unified formatter for many languages. I suppose it might be nice for a solution to have support for languages other than Java, but that is not a requirement.

Furthermore, tools that only perform code highlighting are right out.

I'm also not interested in a web service. I want a tool that I can run locally.

Finally, solutions need not be restricted to open source, public domain, shareware, free software, commercial, or anything else. All forms of licensing are acceptable.

like image 546
Greg Mattes Avatar asked Jun 15 '09 15:06

Greg Mattes


2 Answers

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

like image 200
Jon Avatar answered Oct 15 '22 04:10

Jon


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.

like image 23
notnoop Avatar answered Oct 15 '22 05:10

notnoop