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

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.


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)


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


Try "Jacobe Code Beautifier":

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

The personal edition is free.