eclipse 可以配置为自动修复丢失的导入吗?

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

Can eclipse be configured to automatically fix missing Imports?

eclipse

提问by NorthIsUp

Because they didn't make it easy enough already ;)

因为他们已经做得不够容易了;)

回答by Fabian Steeg

I think you are looking for Ctrl+ Shift+ O

我想你正在寻找Ctrl+ Shift+O

You can also set up Eclipse to automatically organize imports on save:

您还可以设置 Eclipse 以在保存时自动组织导入:

Preferences> Java> Editor> Save Actions

Preferences> Java> Editor>Save Actions

回答by Andrii Karaivanskyi

Ctrl + Shift + O (Cmd + Shift + O for mac) - Organize all imports
Ctrl + Shift + M (Cmd + Shift + M for mac) - Import one by one (you need cursor on the red marker)

Ctrl + Shift + O (Cmd + Shift + O for mac) - 组织所有导入
Ctrl + Shift + M (Cmd + Shift + M for mac) - 一一导入(你需要将光标放在红色标记上)

回答by incorelabs

Eclipse: Automatically fix missing imports

Eclipse:自动修复丢失的导入

1. macOS

1. macOS

a) Organize all imports

a) 组织所有进口

Command+ Shift+ O

Command+ Shift+O

b) Add import for a specific line

b) 为特定行添加导入

Command+ Shift+ M

Command+ Shift+M

2. Windows

2. 窗户

a) Organize all imports

a) 组织所有进口

Control (Ctrl)+ Shift+ O

Control (Ctrl)+ Shift+O

b) Add import for a specific line

b) 为特定行添加导入

Control (Ctrl)+ Shift+ M

Control (Ctrl)+ Shift+M

回答by hram908

From Eclipse Menu, Select Source-->Organize Imports. This will remove all unused imports from that Java file.

从 Eclipse 菜单中,选择 Source-->Organize Imports。这将从该 Java 文件中删除所有未使用的导入。

回答by James Branigan

@Fabian's answer is correct if the class to be imported is already on the classpath for the java project or plugin.

如果要导入的类已经在 java 项目或插件的类路径上,@Fabian 的回答是正确的。

I'm not aware of a way to get Require-Bundle headers added(in the case of eclipse plugins) or package imports(in the case of osgi bundles) added automatically by Eclipse.

我不知道有什么方法可以让 Eclipse 自动添加 Require-Bundle 标头(在 Eclipse 插件的情况下)或包导入(在 osgi 包的情况下)。

What this means is that you can get what you want as long as you restrict the classes you add to those already on the classpath. However, if you try to start using a class that isn't already on your path, the organize imports aren't going to be able to help you.

这意味着只要您将添加的类限制为类路径上已有的类,您就可以获得想要的东西。但是,如果您尝试开始使用不在您的路径上的类,组织导入将无法帮助您。

回答by NorthIsUp

I found this site: http://eclipseone.wordpress.com/2010/03/10/navigate-and-fix-errors-amp-warnings-in-a-class-with-eclipse-keyboard-shortcuts/

我找到了这个网站:http: //eclipseone.wordpress.com/2010/03/10/navigate-and-fix-errors-amp-warnings-in-a-class-with-eclipse-keyboard-shortcuts/

It recommends using ctr/cmd + 1 to use the keyboard, this is much better than having to use the mouse, but I'm still looking for a "Just do the right thing most of the time" automatic solution.

它建议使用 ctr/cmd + 1 来使用键盘,这比必须使用鼠标要好得多,但我仍在寻找“大多数时候只做正确的事情”的自动解决方案。