Formatting your code using the Eclipse Code Formatter

Google Search Results

You arrived here after searching for the following phrases:

Click a phrase to jump to the first occurrence, or return to the search results.

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 <path to your workspace>/<yourproject>/.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

Thanks for reading this post. You should follow me on twitter here

    • AlexP
    • March 12th, 2009

    Is it possible to utilize 2 different formatters on the same project?
    With shortcuts or menu?

  1. Alex, you can only use one formatter per project.

    • mike
    • November 13th, 2009

    would you know how to batch format c/c++ code from the command line? i have been searching on how to do this, but i only see it shown for java code. thanks!

    • Arun
    • February 11th, 2010

    can you let me know the command line for formatting c++ code in eclipse cdt?

    • Phil
    • February 25th, 2010

    this should work for the CDT as well by implementing a class like org.eclipse.jdt.core.formatter.CodeFormatterApplication for the CDT’s CodeFormatter

  1. November 3rd, 2009

Additional comments powered by BackType