Skip to content

May 28, 2007

19

Formatting your code using the Eclipse Code Formatter

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. Follow me on twitter here to be notified about updates and other posts I write. Or, subscribe to my RSS feed here

Fork me on GitHub
Read more from Eclipse, Tools
  • AlexP

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

  • http://www.peterfriese.de Peter

    Alex, you can only use one formatter per project.

  • http://www.peterfriese.de/getting-started-with-xtext-part-2/ Getting started with Xtext, part 2 – Peter Friese

    [...] the formatter is based on the Eclipse java formatter (see also this post on the Eclipse code formatter which I wrote some years ago and which still drives 200 reader/day to my blog), we have to add some [...]

  • mike

    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

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

  • Phil

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

  • mahesh

    thanks a lot.. it was helpful

  • Aniruddha

    This was excellent. Keep posting

  • ddd

    Great !!! Really helpful.

  • Ximon Eighteen

    Hi all, I know this is a very old post but I just found it and tried it out with Eclipse Helios 3.6. It worked for me but I needed to add -data /path/to/myworkspace otherwise I got error “org.eclipse.core.internal.dtree.ObjectNotFoundException: Tree element ‘/maven_war/target/war/work/webapp-cache.xml’ not found” in ~/.workspace/.log.

  • Lupin

    Hello, i know this is an old thread, but i’m looking to implement the same actions but to format xml src files, specifically ant src files. Does eclipse also have a formatter class similar to jdt.core.JavaCodeFormatter that i could use to replicate the steps mentioned above?, if not, any suggestions?

  • Kam Mao

    great topic, it helps me alot

  • http://www.seekphp.com/script-or-program-for-indenting-automatically-java-and-html-files/ Script or program for indenting automatically Java and HTML files | SeekPHP.com

    [...] you are looking for command line invocation, read this link. Copy pasting for [...]

  • Zubermds

    gr8 topic

  • Tskumarme

    Hi.. Is there any way to load my customized code formatter xml file by code with the latest profile name and preview? I wanted to package my eclipse with default code formatter as my own and don’t want to setup by clients everytime. Kindly provide me a solution.

  • Jigarr_patel03

    Hello Every Body Good morning   in this Blog…
    Mine New Entry….
    Que::  I want to create Custom Code Foratting file to use in java for formating my Java
    Application code ….
    How can i create this…????
    Any Idea???

  • http://www.register-web-domain.in Domain registration

    Great information  You share a ton of interesting information keep sharing more articles

  • divya

    hii every1 ….
    how the code formatting effect in eclipse

  • http://www.hancocks.co.uk/ Emma Mall

    Useful tutorial indeed! It’s really impressive stuffs formatting code using the eclipse code formatter. Thanks for published very useful suggestion. Keep up coming info!