如何从 Eclipse 中删除未使用的导入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5686825/
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 20:08:19  来源:igfitidea点击:

How to remove unused imports from Eclipse

eclipse

提问by Heisenbug

Is there any way to automatically remove all unused imports (signaled with a warning) of a project with Eclipse IDE?

有什么方法可以使用 Eclipse IDE 自动删除项目的所有未使用的导入(带有警告信号)?

回答by Heisenbug

I just found the way. Right click on the desired package then Source-> Organize Imports.

我刚刚找到了方法。右键单击所需的包,然后Source-> Organize Imports

Shortcut keys:

快捷键:

  • Windows: Ctrl+ Shift+ O
  • Mac: Cmd+ Shift+ O
  • Windows系统:Ctrl+ Shift+O
  • 苹果:Cmd+ Shift+O

回答by Asad Nauman

You can direct use the shortcut by pressing Ctrl+Shift+O

您可以通过按Ctrl+ Shift+直接使用快捷方式O

回答by NewUser

I know this is a very old thread. I found this way very helpful for me:

我知道这是一个非常古老的线程。我发现这种方式对我很有帮助:

  1. Go to Window → Preferences → Java → Editor → Save Actions.
  2. Check the option "Perform the selected actions on save".
  3. Check the option "Organize imports".
  1. 转到窗口 → 首选项 → Java → 编辑器 → 保存操作。
  2. 选中“在保存时执行选定的操作”选项。
  3. 选中“组织导入”选项。

Now every time you save your classes, eclipse will take care of removing the unused imports.

现在每次保存类时,eclipse 都会处理删除未使用的导入。

回答by user2589779

Remove all unused import in eclipse:

删除 eclipse 中所有未使用的导入:

Right click on the desired package then Source->Organize Imports.Or You can direct use the shortcut by pressing Ctrl+Shift+O

右键单击所需的包,然后单击Source->Organize Imports。或者您可以通过按Ctrl+Shift+O直接使用快捷方式

Work perfectly.

完美工作。

回答by niknik123

If you want to do this on whole project then inside project -->select src folder-->press Ctrl+Shift+O

如果要在整个项目中执行此操作,请在项目内部 --> 选择 src 文件夹-->按Ctrl+Shift+O

回答by MobileAppDeveloper

Use ALT + CTRL + O. It will organize all the imports. You can find various other options in the "Code" Menu.

使用 ALT + CTRL + O。它将组织所有导入。您可以在“代码”菜单中找到各种其他选项。

EDIT: Sorry it is CTRL + SHIFT + O

编辑:对不起,它是 CTRL + SHIFT + O

回答by Rakesh Singh Balhara

press Ctrl+Shift+Oand it will remove unwanted imports

Ctrl+Shift+O它将删除不需要的导入

回答by Jesus Dimrix

Better way is just to add "save action" so when you save the project it will clear the unused import's and format the code as well if you like .

更好的方法是添加“保存操作”,因此当您保存项目时,它将清除未使用的导入并格式化代码(如果您愿意)。

Go to Window > Preferences > Java > Editor > Save Actions

转到窗口 > 首选项 > Java > 编辑器 > 保存操作

and pick what ever you want .

并选择您想要的任何东西。

回答by Mykhaylo Adamovych

Not to reorganize imports (not to unfold .* and not to reorder lines) to have least VCS changeset
you can use custom eclipse clenup as this answersuggests

不重新组织导入(不展开 .* 并且不重新排序行)以获得最少的 VCS 变更集,
您可以使用自定义 eclipse clenup 作为这个答案建议

回答by Andy

Certainly in Eclipse indigo, a yellow line appears under unused imports. If you hover over that, there will be multiple links; one of which will say "Remove unused import". Click that.

当然,在 Eclipse indigo 中,未使用的导入下方会出现一条黄线。如果你将鼠标悬停在它上面,就会有多个链接;其中之一会说“删除未使用的导入”。单击那个。

If you have multiple unused imports, just hover over one and there will be a link that allows you to remove all unused imports at once. I can't remember the exact wording off hand, but all the links that appear are pretty self explanatory.

如果您有多个未使用的导入,只需将鼠标悬停在一个上,就会出现一个链接,可让您一次删除所有未使用的导入。我不记得手头的确切措辞,但所有出现的链接都是不言自明的。