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

9

92

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.

Knighton answered 15/6, 2009 at 15:7 Comment(1)
I whipped something up that is a standalone Helios (Eclipse 3.6) that only does code formatting. It is <13 MB, Java only. openmrs-mailing-list-archives.1560443.n2.nabble.com/…Otten
M
25

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

Modestomodesty answered 15/6, 2009 at 15:11 Comment(4)
What's the relationship between the open source Jalopy and this one: triemax.com/products/jalopyKnighton
It looks like the open source project is dead and they've decided to go with a commercial version - produced by Triemax. It's been a long time since I used Jalopy, I believe it was with Ant. Take a look at Jacobe instead.Modestomodesty
Jacobe is not being updated for Java 7, and TIOBE says it won't ever be.Mossman
There's also google-java-format.Epithelioma
P
14

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.

Paly answered 16/7, 2009 at 14:36 Comment(0)
A
7

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)

Armstead answered 15/6, 2009 at 15:12 Comment(0)
S
4

UPDATE: Per my new understanding based on an incorrect bug report I filed: https://bugs.eclipse.org/bugs/show_bug.cgi?id=325289 The OpenMRSFormatter.xml file actually has to be a preferences file and not an XML file.

You can find such a file here: https://bugs.eclipse.org/bugs/attachment.cgi?id=178874

or convert any XML exported formatter file with: cat formatter.xml | grep setting | sed 's/@@' > formatter.asprefs

Thank you Misha


Here is something that I made using Eclipse 3.6 jars. It is Java-only, command line, and should be cross platform compatible:

http://www.sendspace.com/file/nm4ptt

There is a shell script that uses a bundled OpenMRSCodeFormatter.xml file. The sample output:

misha@misha-d630:~/openmrs-code-formatter$ cat ../Test.java
public class Test {



public a() {
System.out.println("! I did not format this right!");
}



}misha@misha-d630:~/openmrs-code-formatter$ ./format.sh ../Test.java
Configuration Name: OpenMRSFormatter.xml
Starting format job ...
Done.
cat ../misha@misha-d630:~/openmrs-code-formatter$ cat ../Test.java
public class Test {

        public a() {
                System.out.println("! I did not format this right!");
        }

}misha@misha-d630:~/openmrs-code-formatter$ 

This is based on the post here: http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/

As I do not know how long the link will stay up, I will describe the contents below.

This is based on Eclipse 3.6 Helios distribution. I used the one for Linux x86, but it should work for any version, as I did not use any (to my knowledge) platform specific JAR files.

The contents of the tar archive are:

misha@misha-d630:~$ tar tvjf openmrs-code-formatter.tar.bz2  | sort 
tar: Record size = 8 blocks
drwxr-xr-x misha/misha       0 2010-09-11 19:32 openmrs-code-formatter/plugins/
drwxr-xr-x misha/misha       0 2010-09-11 19:33 openmrs-code-formatter/configuration/org.eclipse.equinox.simpleconfigurator/
drwxr-xr-x misha/misha       0 2010-09-11 19:38 openmrs-code-formatter/configuration/
-rw-r--r-- misha/misha  101957 2010-09-11 19:03 openmrs-code-formatter/plugins/org.eclipse.equinox.common_3.6.0.v20100503.jar
-rw-r--r-- misha/misha  107210 2010-09-11 19:32 openmrs-code-formatter/plugins/org.eclipse.core.commands_3.6.0.I20100512-1500.jar
-rw-r--r-- misha/misha  111098 2010-09-11 19:06 openmrs-code-formatter/plugins/org.eclipse.equinox.preferences_3.3.0.v20100503.jar
-rw-r--r-- misha/misha 1147220 2010-09-11 18:45 openmrs-code-formatter/plugins/org.eclipse.osgi_3.6.0.v20100517.jar
-rw-r--r-- misha/misha    1812 2010-09-11 19:33 openmrs-code-formatter/configuration/org.eclipse.equinox.simpleconfigurator/bundles.info
-rw-r--r-- misha/misha  182596 2010-09-11 19:05 openmrs-code-formatter/plugins/org.eclipse.equinox.registry_3.5.0.v20100503.jar
-rw-r--r-- misha/misha  248071 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.text_3.5.0.v20100601-1300.jar
-rw-r--r-- misha/misha     265 2010-09-11 19:21 openmrs-code-formatter/configuration/config.ini
-rw-r--r-- misha/misha   28554 2010-09-11 19:34 openmrs-code-formatter/OpenMRSFormatter.xml
-rw-r--r-- misha/misha   39331 2010-09-11 19:17 openmrs-code-formatter/plugins/org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503.jar
-rw-r--r-- misha/misha 4504121 2010-09-11 19:27 openmrs-code-formatter/plugins/org.eclipse.jdt.core_3.6.0.v_A58.jar
-rw-r--r-- misha/misha   47023 2010-09-11 18:42 openmrs-code-formatter/plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar
-rw-r--r-- misha/misha   52710 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.core.filesystem_1.3.0.v20100526-0737.jar
-rw-r--r-- misha/misha 6451211 2010-09-11 19:32 openmrs-code-formatter/plugins/com.ibm.icu_4.2.1.v20100412.jar
-rw-r--r-- misha/misha   70928 2010-09-11 19:01 openmrs-code-formatter/plugins/org.eclipse.core.runtime_3.6.0.v20100505.jar
-rw-r--r-- misha/misha  769898 2010-09-11 19:29 openmrs-code-formatter/plugins/org.eclipse.core.resources_3.6.0.v20100526-0737.jar
-rw-r--r-- misha/misha   83817 2010-09-11 19:07 openmrs-code-formatter/plugins/org.eclipse.equinox.app_1.3.0.v20100512.jar
-rw-r--r-- misha/misha   86986 2010-09-11 19:31 openmrs-code-formatter/plugins/org.eclipse.core.expressions_3.4.200.v20100505.jar
-rw-r--r-- misha/misha   90837 2010-09-11 19:05 openmrs-code-formatter/plugins/org.eclipse.core.jobs_3.5.0.v20100515.jar
-rw-r--r-- misha/misha   91928 2010-09-11 19:06 openmrs-code-formatter/plugins/org.eclipse.core.contenttype_3.4.100.v20100505-1235.jar
-rwxr-xr-x misha/misha     111 2010-09-11 19:39 openmrs-code-formatter/format.sh

Relevant jars in plugins directory are taken directly from Eclipse 3.6 plugins directory.

The contents of config.ini are:

misha@misha-d630:~$ more openmrs-code-formatter/configuration/config.ini 
osgi.bundles=reference\:file\:org.eclipse.equinox.simpleconfigurator_1.0.200.v20100503.jar@1\:start
org.eclipse.equinox.simpleconfigurator.configUrl=file\:org.eclipse.equinox.simpleconfigurator/bundles.info
eclipse.application=org.eclipse.jdt.core.JavaCodeFormatter

format.sh is:

misha@misha-d630:~$ more openmrs-code-formatter/format.sh 
#!/bin/bash
java -jar plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar -config OpenMRSFormatter.xml $*

and OpenMRSFormatter.xml is available here: http://wiki.openmrs.org/download/attachments/3346087/OpenMRSFormatter.xml?version=1&modificationDate=1282168038000

I believe that should be sufficient to reproduce.

An interesting project based on the same concept is found here: http://code.google.com/p/maven-java-formatter-plugin/

Thank you Misha

Sponsor answered 12/9, 2010 at 18:20 Comment(0)
D
1

Try "Jacobe Code Beautifier":

http://www.tiobe.com/index.php/content/products/jacobe/Jacobe.html

The personal edition is free.

Degust answered 6/8, 2009 at 21:43 Comment(1)
Another online free one: tutorialspoint.com/online_java_formatter.htmReheat
L
0

I used to use Dr. Java to code in and with a quick ctrl+A + tab it formats it all nicely with syntax highlighting. Ive used it on a Mac and Windows, dont know if they support linux. And it's open source, stand alone, and free.

Landlordism answered 15/6, 2009 at 15:15 Comment(2)
Pressing tab when any part of the code is selected will format the selection. DrJava also a nice interactive (interpreter like) tab.Mystify
This did not work for me using version drjava-beta-2019-220051Reheat
A
0

I also used a lot the PrettyPrinter embedded inside JRefactory (http://jrefactory.sourceforge.net/). I discovered it through its plugin for JEdit. I appreciated it as it had the better javadoc coverage I ever saw.

Unfortunately it seems dead (no evolution since 2004) and actually it does not support Java5.

By the way I confirm that Triemax is a commercial suite based on dead open-source Jalopy.

Anastigmatic answered 15/6, 2009 at 20:1 Comment(0)
O
0

See our Java Formatter for a tool that handles all dialects of Java, including version 7.

Olgaolguin answered 5/5, 2012 at 17:1 Comment(0)
D
0

Jalopy does not handle Statements like List stuff<MyClass> ... and lint comments starting with @

I have been able to force it to format by changing all the <MyClass> into LTLTLTMyClassGTGTGT and then @ into //@//@//@ (I just repeat the LT, GT or //@ to make sure they don't already exist. Then I was able to Jalopy, and obviously I have to replace everything back to what it was.

Also, prior to changing the < or > you have to make sure you won't change compares like <= or >= etc.

So, you might call this the poor man solution because it involves many manual steps. If I was to do Jalopy more often, I would probably write a script using sed that would make those changes automatically for me.

Defelice answered 23/10, 2014 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.