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.


Some people often ask me the question : what is your opinion Olivier ? With this blog, I'll try to anwer some of those questions.
The main subjects of this blog will be IT architecture, SOA, Java and related frameworks, Open Source, Web technologies, ...
You can also consult the french version of this blog on TEPQO?.