NorthIsUp
NorthIsUp

Reputation: 17867

Can eclipse be configured to automatically fix missing Imports?

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

Upvotes: 45

Views: 70492

Answers (6)

incorelabs
incorelabs

Reputation: 586

Eclipse: Automatically fix missing imports

1. macOS

a) Organize all imports

Command + Shift + O

b) Add import for a specific line

Command + Shift + M

2. Windows

a) Organize all imports

Control (Ctrl) + Shift + O

b) Add import for a specific line

Control (Ctrl) + Shift + M

Upvotes: 9

Andrii Karaivanskyi
Andrii Karaivanskyi

Reputation: 2002

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)

Upvotes: 6

hram908
hram908

Reputation: 394

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

Upvotes: 3

James Branigan
James Branigan

Reputation: 1154

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

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.

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.

Upvotes: 1

Fabian Steeg
Fabian Steeg

Reputation: 45714

I think you are looking for Ctrl + Shift + O

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

Preferences > Java > Editor > Save Actions

Upvotes: 87

NorthIsUp
NorthIsUp

Reputation: 17867

I found this site: 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.

Upvotes: 0

Related Questions