43

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

NorthIsUp
  • 17,502
  • 9
  • 29
  • 35

6 Answers6

86

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

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
7

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

incorelabs
  • 566
  • 5
  • 13
6

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)

Andrii Karaivanskyi
  • 1,942
  • 3
  • 19
  • 23
2

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

hram908
  • 374
  • 6
  • 15
1

@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.

James Branigan
  • 1,164
  • 7
  • 9
0

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.

NorthIsUp
  • 17,502
  • 9
  • 29
  • 35