如何一次性格式化Eclipse项目中的所有Java文件?
我有一个旧的 Eclipse 项目,代码格式不正确。我想根据 Eclipse 中的设置格式化所有 .java
文件。我不想使用 Ctrl+Shift+F 编辑每个单独的文件。有没有办法格式化我的所有文件?也许是 Eclipse 插件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Eclipse Mars.1 Release (4.5.1) 及更高版本中,只需按
右键单击代码输入区域
如果您想格式化单个
java
/xml
文件,只需执行 Ctrl+Shift+fIn Eclipse Mars.1 Release (4.5.1) version and above, just press
Right click on the code typing area
If you want to format individual
java
/xml
file just do Ctrl+Shift+f右键单击项目根目录并选择 Source ->格式。这至少适用于 3.8.1 版本。及以上。
如果上述方法不起作用,您可能使用的是较旧的 Eclipse 版本。在这种情况下,您可以通过在按住 CTRL 的同时单击源文件夹来选择源文件夹,然后选择源 -> 源文件夹。从右键菜单中设置格式。如果您不想格式化整个项目,也可以使用包文件夹和类文件。
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.
我还打开了 Java Perspective,但这仍然不起作用。我的 eclipse 仅格式化 css 和 Html 文件,但不格式化 JAVA 文件。
然后,我找到这个答案解释了这一点:
然后,您应该检查
PROJECT-PROPERTIES-PROJECT FACETS
并激活 Java Facet项目:右键单击
PACKAGE-EXPLORER
顶部,您将找到 SOURCE - FORMAT 选项,它也会格式化 Java 文件。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:
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.这就是目的
选择适当的选项。
This is the purpose of
selecting the appropriate options.
在旧版本的 Eclipse (Indigo) 中,它可以从 Package Explorer(而不是 Navigator)运行。右键单击要格式化的包,然后选择“源”->“格式。它将格式化该包及其子包中的所有类。
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.