如何一次格式化 Eclipse 项目中的所有 Java 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5133781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:59:15  来源:igfitidea点击:

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

eclipsecode-formatting

提问by sesamegu

I have an old Eclipse project and the code is not well formatted. I'd like to format all the .javafiles 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?

我有一个旧的 Eclipse 项目,代码格式不正确。我想.java根据 Eclipse 中的设置格式化所有文件。我不想编辑与每一个人文件Ctrl+ Shift+ F。有没有办法格式化我的所有文件?也许是一个 Eclipse 插件?

回答by esaj

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

右键单击项目根目录并选择 Source -> Format。这应该至少适用于 3.8.1 版。以上。

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.

如果上述方法不起作用,则您可能使用的是较旧的 Eclipse 版本。在这种情况下,您可以通过在按住 CTRL 的同时单击它们来选择源文件夹,然后从右键单击菜单中选择源 -> 格式。也适用于包文件夹和类文件,以防您不想格式化整个项目。

回答by Alejandro Teixeira Mu?oz

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.

我还打开了 Java Perspective,但仍然无法正常工作。我的日食只是格式化 css 和 Html 文件,而不是 JAVA 文件。

Then, I located this answerthat explains this:

然后,我找到了解释这一点的答案

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

您必须将 Java Facet 添加到项目中以允许 Eclipse 格式化程序检测 Java 类

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

然后,您应该检查PROJECT-PROPERTIES-PROJECT FACETS并激活项目的 Java 方面:

enter image description here

在此处输入图片说明

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

通过右键单击顶部,PACKAGE-EXPLORER您将找到 SOURCE - FORMAT 选项,它也会格式化 Java 文件。

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

小心,检查它的包资源管理器,而不是项目资源管理器(单击 Window-Show View-PackageExplorer)

enter image description here

在此处输入图片说明

回答by bmargulies

This is the purpose of

这是目的

Source ->Clean Up...

来源 -> 清理...

selecting the appropriate options.

选择适当的选项。

回答by Rares Oltean

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.

在旧版本的 Eclipse (Indigo) 中,它可以通过 Package Explorer(而不是 Navigator)工作。右键单击要格式化的包,然后选择 Source -> Format。它将格式化该包及其子包中的所有类。

回答by user3572104

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

在 Eclipse Mars.1 Release (4.5.1) 及以上版本中,只需按 Right click代码输入区

Src > Source > Format.

源 > 源 > 格式。

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

如果你想格式化个人java/xml文件就做Ctrl+ Shift+f