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






Is it possible to utilize 2 different formatters on the same project?
With shortcuts or menu?
Alex, you can only use one formatter per project.
[...] 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 [...]
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!
can you let me know the command line for formatting c++ code in eclipse cdt?
this should work for the CDT as well by implementing a class like org.eclipse.jdt.core.formatter.CodeFormatterApplication for the CDT’s CodeFormatter
Is it possible already exising project. But i have applied my existing project, but it’s not reflecting my existing java code. is it possible to apply my existing code also whatever i have setted for code style?
thanks a lot.. it was helpful
Peter,
I’m looking for something like this that formats files beyond just Java source. In other words, jsp, html, xml etc… Does Eclipse handle those types of files as well?
Thanks,
Joe
This was excellent. Keep posting
Great !!! Really helpful.
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.
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?
great topic, it helps me alot
[...] you are looking for command line invocation, read this link. Copy pasting for [...]
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.
hii every1 ….
how the code formatting effect in eclipse
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!
Hi, thanks there! it’s great tutorial.
I follow the steps but the console turned out saying:
“The Eclipse formatter failed to format xxx.java”. Skip the file.
It skip all the 67 java files in the target directory. Could you give me some clue here?
Thank you.
btw. I’m using Mac
Ha, I found how to fix it!
I forgot to add these three lines into prefs file(it’s 2012, and jdk has updated to 1.6 now):
org.eclipse.jdt.core.compiler.compliance=1.6org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6org.eclipse.jdt.core.compiler.source=1.6
[...] J’ai trouvé la solution dans http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line et http://www.peterfriese.de/formatting-your-code-using-the-eclipse-code-formatter/ par contre je préfère sauter l’étape d’enregistrer le formatage à un projet et utiliser le formattage affecter au workspace, ma ligne de commande ressemble donc plutôt à ça : [...]
Great tips ! thx for sharing it , i am almost giving up with my ugly formatted code .
hello, thanks a lot for this. Its really usefull.
one last thing: did you find a way to use eclipse’s “organize import” standalone?
greetings,
marco
Hi Marco,
unfortunately, there is no direct way of organizing imports from the command line.
Check out this thread http://www.eclipse.org/forums/index.php/m/85243/ to get an idea of how to use OrganizeImportsOperation to organize imports. Be aware, however, that this might only be possible in a UI.
Cheers,
Peter