Friday, 25 May 2007

Batch formatting Java source code with Eclipse

I've missed the news, but it can be extremely useful : since version 3.2 of Eclipse, it is possible to apply the formatting of Java code to a set of source files. This functionality is accessible via the command line.

The following example runs the formatting of code specified by the configuration file D:/formatter.prefs on the files Java belonging (directly or indirectly) to the directory D:/tmp/src :

eclipse -application org.eclipse.jdt.core.JavaCodeFormatter -config D:/formatter.prefs D:/tmp/src

The code formatter configuration file can be obtained in the following way:
  1. In Eclipse, open the properties of a Java project.
  2. In Code Style > Formatter, check Enable project specific settings.
  3. Select and edit a formatter configuration.
  4. Click on OK.
  5. The code formatter configuration file will then be in your Eclipse workspace in workspace/YourJavaProject/.settings/org.eclipse.jdt.core.prefs.

9 comments:

Anonymous said...

I have managed to seperate it own, so that you don't need to have eclipse installed. I have also made an ANT task of it too. You can download it from http://www.darkedges.com/java/ant/JavaCodeFormatter.zip

Anonymous said...

Interesting to know.

Gaël Marziou said...

Exactly the information I was looking for.
Thanks for this post

Anonymous said...

Try the open source project jastyle, a 100% pure java port of the astyle. It is very small (<70Kb) and it could be integrated easily. http://sourceforge.net/projects/jastyle/

Anonymous said...

you can also use workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs

if you just want to use the default settings

Anonymous said...

Willkommen und Hallo in unseren Sex-Chat.



Der Sex-Chat bietet ihnen die Möglichkeit geile votzen kostenlos und natürlich vieles mehr,wie sexuellen Vorlieben ausleben
Hier im besten Sex-Chat findest du geile votzen kostenlos Flirt und Sextalk
Du suchst Live Dates , sicher bist du hier genau richtig.Gut,worauf wartest du?
Blind Date chat kommunikation ,anmelden .
Suchst du jemand in Neuenburg, vieleicht in deutschland, oder aus Aarau , oder in Binningen, oder Ecublens? Bestimmt ist da jemand dabei.!

Anonymous said...

top [url=http://www.001casino.com/]free casino[/url] coincide the latest [url=http://www.realcazinoz.com/]casino[/url] free no consign reward at the best [url=http://www.baywatchcasino.com/]online casinos
[/url].

Unknown said...

Like code formating, is there any way to execute Code Cleanup using commandline?

Unknown said...

I want to do the same but with organizing imports, do you have any idea of the parameter i should pass after -application ?