How to import class automatically in Eclipse

In Eclipse, to import a single class, you may clicked on the class and press shortcut key “Ctrl + Space” to prompt all the available classes to import. However, if you copied a Java source code from somewhere and more than 20+ classes are not import, are you going to press “Ctrl + Space” one by one?

In above situation, Eclipse comes with a nice feature called “Organize Imports” to imports all the classes that are used, automatically.

A copied source code without imports.

import class in eclipse automatically

To import above classes automatically, you can select “Source” > “Organize Imports” or press shortcut “Ctrl + Shift + O“. See result :

import class in eclipse automatically

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Pali
13 years ago

Thanks, it helped!

John
11 years ago

It worked once but now instead of adding it just re-organzes my imports… any help?

Anders
4 years ago

This doesn’t work for me. I get the “java.lang.IllegalArgumentException: Sources must not be empty” exception and Tomcat crashes when running this as a regular java application 🙁

Anders
4 years ago
Reply to  Anders

Sorry. Clicked on the wrong tab this is meant for somewhere else.

omar
8 years ago

This feature it was available in older eclipse versions, but in oxygen 4.7 it is not longer available, i must go to “Source” > “Organize Imports”

Praveen Kumar
7 years ago
Reply to  omar

sir which class is available to all the class automatically

Matt
13 years ago

Doesn’t work for me. The imports are not added.