177

I have an old Eclipse project and the code is not well formatted. I'd like to format all the .java files according to the settings in Eclipse. I don't want to edit every individual file with Ctrl+Shift+F. Is there a way to format all my files? Perhaps an Eclipse plugin?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
sesamegu
  • 1,773
  • 2
  • 10
  • 4

5 Answers5

272

Right click on the project root and select Source -> Format. This should work for at least version 3.8.1. and above.

If the above does not work, you're probably using an older Eclipse-version. In such case you can select your Source Folders by clicking on them while holding down CTRL, then select Source -> Format from the right-click -menu. Works with package-folders and class files also, in case you don't want to format the entire project.

esaj
  • 15,875
  • 5
  • 38
  • 52
  • 2
    Selecting project root worked fine for me to format all files in project, using Eclipse 3.8.1. – Jason McVetta Jun 10 '13 at 22:18
  • 1
    +1 selecting the project seems to work in eclipse 4.2 also. Consider removing the strikethrough and editing the answer to include the alternate solution for those who encounter a problem selecting the project. – Luke Jul 02 '13 at 19:26
  • 1
    @Luke and Jason McVetta: Thanks for the info, I've edited the answer to mention that selecting project root works in newer Eclipses and made the second paragraph a bit clearer. – esaj Jul 03 '13 at 06:53
  • 9
    Actually, upon further testing I have determined that the difference is Package Explorer vs. Project Explorer. In Project Explorer, it doesn't format all the files and in Package Explorer it does. – Luke Jul 03 '13 at 14:21
  • doesn't this has to auto format itself without having the option to use source->format?? – Ikshvak Mar 03 '14 at 22:03
  • 1
    This also formats other files in the the project like .xml, .css and .html. Is there a way to only format the .java files? – Kuchi Sep 17 '14 at 12:37
  • @Ikshvak autoformat is a thing, but when you change the formatting of your project, you need to reformat everything and this is not automatic – yunandtidus Oct 07 '14 at 09:22
  • wooow thx a lot !! this is very helpful ... btw I'm using Luna Service Release 1a (4.4.1) , It worked when I selected all the packages then right click, Source, Format – Chris Sim Sep 17 '15 at 06:18
  • Now I worked with Mars Release (4.5.0) and it worked by selecting the classes, than Source - Format – Chris Sim Feb 26 '16 at 10:58
16

I also had the Java Perspective ON and this was still not working. My eclipse was only formatting css and Html files, but not JAVA ones.

Then, I located this answer that explains this:

You must add Java Facet to the project to allow Java classes to be detected by Eclipse formatter

Then, you should check on PROJECT-PROPERTIES-PROJECT FACETS and activate the Java facet for the project:

enter image description here

And by right-clicking on top of the PACKAGE-EXPLORER you will find the SOURCE - FORMAT option, and it will format Java files too.

Be careful, check it´s package explorer, not Project Explorer (click Window-Show View-PackageExplorer)

enter image description here

9

This is the purpose of

Source ->Clean Up...

selecting the appropriate options.

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
bmargulies
  • 97,814
  • 39
  • 186
  • 310
4

In older versions of Eclipse (Indigo) it works from Package Explorer (not Navigator). Right click the package you wish to format then choose Source -> Format. It will format all classes in that package and its sub-packages.

Rares Oltean
  • 606
  • 6
  • 14
1

In Eclipse Mars.1 Release (4.5.1) version and above, just press Right click on the code typing area

Src > Source > Format.

If you want to format individual java / xml file just do Ctrl+Shift+f

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58