How to format all Java files in an Eclipse project at one time?

房东的猫 提交于 2019-11-28 15:26:39

问题


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?


回答1:


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.




回答2:


This is the purpose of

Source ->Clean Up...

selecting the appropriate options.




回答3:


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:

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)




回答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.




回答5:


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



来源:https://stackoverflow.com/questions/5133781/how-to-format-all-java-files-in-an-eclipse-project-at-one-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!