Peter Friese

Developer Advocate / Mobile Developer / Public Speaker

Formatting Your Code Using the Eclipse Code Formatter

| Comments

In case you needed to format a batch of code using a command line utility, you probably went for Jalopy or JIndent. Thanks to Ben Konrath of Red Hat, you no longer need to do so: since Eclipse 3.2, you can use the built-in code formatter to format your Java code using the command line.

Here’s how:

The hardest part of it all is to create the config file for the formatter. To create it, select one of your existing projects, and activate project specific formatter settings (Properties –> Java Code Style –> Formatter –> Enable project specific settings):

Configure the code formatter as desired. Click OK when you’re done.

Using a file explorer, navigate to //.settings and copy org.eclipse.jdt.core.prefs to a new location. This file contains all your formatting settings.

To invoke the code formatter using the command line issue the following command: <path-to-eclipse>\eclipse.exe -vm <path-to-vm>\java.exe -application **org.eclipse.jdt.core.JavaCodeFormatter** -verbose -config <path-to-config-file>\org.eclipse.jdt.core.prefs <path-to-your-source-files>\*.java In case the formatter complains about your code, your code probably contains Java 5 constructs and you have to add the following lines to your config file to make everything work:

org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 org.eclipse.jdt.core.compiler.source=1.5

Comments