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.

3 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

Melvina said...

Interesting to know.

Gaël Marziou said...

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