Any way (or shortcut) to auto import the classes in IntelliJ IDEA like in Eclipse?

186,184

Solution 1

IntelliJ IDEA does not have an action to add imports. Rather it has the ability to do such as you type. If you enable the "Add unambiguous imports on the fly" in Settings > Editor > General > Auto Import, IntelliJ IDEA will add them as you type without the need for any shortcuts. You can also add classes and packages to exclude from auto importing to make a class you use heavily, that clashes with other classes of the same name, unambiguous.

For classes that are ambiguous (or is you prefer to have the "Add unambiguous imports on the fly" option turned off), just type the name of the class (just the name is OK, no need to fully qualify). Use code completion and select the particular class you want:

enter image description here

Notice the fully qualified names to the right. When I select the one I want and hit enter, IDEA will automatically add the import statement. This works the same if I was typing the name of a constructor. For static methods, you can even just keep typing the method you want. In the following screenshot, no "StringUtils" class is imported yet.

enter image description here

Alternatively, type the class name and then hit Alt+Enter or +Enter to "Show intention actions and quick-fixes" and then select the import option.

Although I've never used it, I think the Eclipse Code Formatter third party plug-in will do what you want. It lists "emulates Eclipse's imports optimizing" as a feature. See its instructions for more information. But in the end, I suspect you'll find the built in IDEA features work fine once you get use to their paradigm. In general, IDEA uses a "develop by intentions" concept. So rather than interrupting my development work to add an import statement, I just type the class I want (my intention) and IDEA automatically adds the import statement for the class for me.

Solution 2

Not all at once. But you can press

Alt + Enter

People assume it only works when you are at the particular item. But it actually works for "next missing type". So if you keep pressing Alt + Enter, IDEA fixes one after another until all are fixed.

Solution 3

I think the best solution, though not exactly the same as Eclipse/Netbeans, is to change the 'Optimize Imports' settings.

Under Preferences > Editor > General > Auto Import

Set Add unambiguous imports on the fly

Edit: Using this method, when there are ambiguous imports, IntelliJ will let you know, and you can then use Alt + Enter method outlined in the answer by Wuaner

I find that, almost always, the most appropriate Import is at the top of the list.

Solution 4

Can't import all at once but can use following combination:

ALT + Enter --> Show intention actions and quick-fixes.

F2 --> Next highlighted error.

Solution 5

Seems like IntelliJ IDEA will import missed class automatically, and you can import them by hit Alt + Enter manually.

Share:
186,184
PlainOldProgrammer
Author by

PlainOldProgrammer

Passion for programming and learn the fundamentals.

Updated on July 09, 2021

Comments

  • PlainOldProgrammer
    PlainOldProgrammer almost 3 years

    In Eclipse, while coding in Java and press Ctrl + Shift + O auto import all the Classes automatically.
    In NetBeans, this is done with Ctrl + Shift + I.

    Is any way to do this in IntelliJ IDEA?

    I searched an equivalent shortcut in google, StackOverflow, IntelliJ IDEA configuration and in the official IntelliJ website Keyboard Shortcuts You Cannot Miss

  • GuiSim
    GuiSim about 8 years
    I assume you are referring to the keyboard key symbols. These can be added using the kbd tag<kbd>Alt</kbd> + <kbd>Enter</kbd>
  • Ajak6
    Ajak6 almost 8 years
    Go to the class which it is not able to find and press ⌥+ Enter. Glad that I scrolled down.
  • Adam
    Adam over 7 years
    It's a lot more work this way when you're editing old files.
  • Rodrigo
    Rodrigo over 7 years
    This isn't working for me. When I press Enter after selecting a class that appears in the autocomplete list, IntelliJ doesn't import it. Instead, it writes the fully-qualified name of the class. This is very annoying and useless. Do you know how to fix this?
  • Rodrigo
    Rodrigo over 7 years
    ^ the reason for this behavior is that the class I was trying to import was not in the build path. Once added, pressing Enter correctly imported the class.
  • laylaylom
    laylaylom about 7 years
    Is there a way to remap Alt+Enter to a more finger-friendly combination? I couldn't figure it out through the IntelliJ IDEA > Preferences > Keymap menu.
  • Rahul Saini
    Rahul Saini almost 7 years
    I had come to this question hoping to find any such key shortcut for IntelliJ. The problem is that if I comment a large section of code having classes, IntelliJ automatically removes the namespace imports. Now If I go and uncomment the code, IntelliJ does not re-import the namespaces for the same and it becomes quite a headache to go on doing ALT+Enter for each of them. Any solution?
  • vikramvi
    vikramvi over 6 years
    I found Eclipse way of missing import statement addition easier than IDEA's. I always get confused where to put mouse cursor before typing Alt + Enter
  • atlas_scoffed
    atlas_scoffed almost 6 years
    @Raja I'm not sure why either. I do know that when I told my co-workers they chose not to use this method though... mysterious.
  • Raja Anbazhagan
    Raja Anbazhagan almost 6 years
    The instructions are pretty clear. It only auto imports the unambiguous classes. And I have been using it since past two days on my 3000+ classes project. Works like a charm. Thanks for sharing.
  • osundblad
    osundblad almost 6 years
    Alt+Enter in combination with F2 (Next Highlighted Error) is usually quite quick even for (too) large Classes.
  • Philip Rego
    Philip Rego about 5 years
    This is wrong in at least one case. I just tried it gives options for the current (no errors) line when other lines need import.
  • peekay
    peekay almost 5 years
    I agree with @RahulSaini on this. There is no reason to have two separate key maps for a single activity. I don't want to and shouldn't have to do multiple things to accomplish a single action. I would say that at this point intellij is just being belligerent about it and has no real reason not to "organize" the imports as opposed to "optimizing" the imports. I really want to down vote this answer even though it is technically correct.
  • Florian F
    Florian F over 4 years
    I tried. It doesn't make "organize imports" behave the Eclipse way.
  • PlsWork
    PlsWork over 4 years
    Option + Enter on Mac
  • Luk Aron
    Luk Aron over 4 years
    This should be the accepted answer. Other answers required lot of works.
  • Paul Verest
    Paul Verest about 3 years
    0 Zero or O Ou ? Both did not work on IDEA CE 2019.