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
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://www.darnkitty.com/ Matt

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

  • Pali

    Thanks, it helped!