154

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
PlainOldProgrammer
  • 2,725
  • 5
  • 22
  • 30
  • 2
    possible duplicate of [IntelliJ Idea 12: How do i auto import namespaces?](http://stackoverflow.com/questions/14032680/intellij-idea-12-how-do-i-auto-import-namespaces) – Madhan Jul 03 '15 at 17:34
  • 4
    When looking for a command/keyboard shortcut, type Ctrl-shift-A (or Cmd-shift-A on Mac), and search for the command. For example here, typing "imports" will show you the command "Optimize imports", and its keyboard shortcut. – JB Nizet Jul 03 '15 at 18:27
  • 3
    The "Optimize imports" doesn't work add the Classes – PlainOldProgrammer Jul 03 '15 at 20:44
  • The auto import for classes is working if you enable it under "Settings > Editor > General > Auto Import". – Andrea Bergonzo Oct 29 '17 at 19:06
  • this really is the biggest failing of intellij. it's a pain. getting conflicts and merges on imports is stupid, but intellij cannot easily help you resolve it – bharal Nov 11 '19 at 14:59

12 Answers12

122

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.

Javaru
  • 30,412
  • 11
  • 93
  • 70
  • 14
    It's a lot more work this way when you're editing old files. – Adam Nov 01 '16 at 17:11
  • 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 Jan 31 '17 at 21:53
  • ^ 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. – Rodrigo Jan 31 '17 at 23:30
  • 3
    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? – Rahul Saini Jul 17 '17 at 06:16
  • 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. – peekay Jun 27 '19 at 14:35
80

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
teknopaul
  • 6,505
  • 2
  • 30
  • 24
  • 1
    I assume you are referring to the keyboard key symbols. These can be added using the `kbd` tag`Alt + Enter` – GuiSim May 04 '16 at 14:41
  • 1
    Go to the class which it is not able to find and press ⌥+ Enter. Glad that I scrolled down. – Ajak6 Jun 03 '16 at 00:03
  • 1
    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. – laylaylom Mar 24 '17 at 20:00
  • 1
    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 – vikramvi Feb 07 '18 at 11:46
  • Alt+Enter in combination with F2 (Next Highlighted Error) is usually quite quick even for (too) large Classes. – osundblad Aug 13 '18 at 20:17
  • 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. – Philip Rego Mar 28 '19 at 20:03
  • Option + Enter on Mac – PlsWork Oct 08 '19 at 20:41
  • This should be the accepted answer. Other answers required lot of works. – Luk Aron Oct 21 '19 at 03:13
27

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.

atlas_scoffed
  • 3,542
  • 30
  • 46
  • 1
    @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. – atlas_scoffed Jun 02 '18 at 06:21
  • 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. – Raja Anbazhagan Jun 02 '18 at 17:31
  • When you have ambiguous imports, and you know that you never want to import some of the options, you can exclude hem in "Preferences > Editor > General > Auto Import > Exclude from auto-import and completion". That way, "Add unambiguous imports on the fly" works more often. – Dario Seidl Jul 14 '23 at 09:07
17

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

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

F2 --> Next highlighted error.

Lyncean Patel
  • 2,433
  • 16
  • 15
10

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

icebat
  • 4,696
  • 4
  • 22
  • 36
Wuaner
  • 929
  • 2
  • 14
  • 31
5

Another option is to ask IDEA to behave like eclipse with eclipse shortcut keys. You can use all eclipse shortcuts by enabling this.

Here are the steps:

1- With IDEA open, press Control + `. Following options will be popped up.

enter image description here

2- Select Keymap. You will see another pop-up. Select Eclipse there.

If you don't see "Keymap" in the options, install "Eclipse Keymap" plugin

enter image description here

3- Now press Ctrl + Shift + O. You are done!

Alon
  • 191
  • 2
  • 9
Dexter
  • 4,036
  • 3
  • 47
  • 55
0

Use control+option+L to auto import the package and auto remove unused packages on Mac

NarendraR
  • 7,577
  • 10
  • 44
  • 82
0

Use Alt+Enter for importing a single package or use Alt+Shift+Enter to auto import all the unambiguous packages in the current file.

0

UPDATE

@ntg has given a very smart solution for converting import .* to individual imports in existing files (it is not completely automatic, but still quite low-effort)

  • Go to any import .* statement and press Alt Enter ( return on Mac)
    • a popup will appear to replace the * import with individual imports
  • then just hit Enter again

enter image description here


Original answer

Not sure if this is universal but I've found that after enabling both the following settings (as told in earlier answers), my IntelliJ is able to smartly convert package.* imports into individual imports (in existing files) just by deleting the import com.company.package.* line

  • Add unambiguous imports on the fly
  • Optimize imports on the fly

enter image description here

Essentially it will be able to add all 'unambiguous' imports for us, the rest will have to be resolved manually


I'm using

  • IntelliJ IDEA Ultimate 2022.1 (Build #IU-222.3739.54)
  • MacOS Monterey 12.5.1
y2k-shubham
  • 10,183
  • 11
  • 55
  • 131
0

As others mentioned, IntelliJ auto imports them on the fly (if enabled in the settings).

If you want to do this across an entire project, what you can do is replace all (CTRL+SHIFT+H) occurrences of the class name to include the package too (e.g. MyClass -> com.app.MyClass), and then you can run the "Remove Unnecessary Qualifications" inspection on the whole project by pressing ALT+Enter on some code that was replaced. Then once the inspection is done, just keep clicking the "Replace qualified name with import" button on the right.

I couldn't find any other way to do the project-wide auto import

REghZY
  • 69
  • 1
  • 7
-1

Hover on top of the code which needs a class then press

alt + shift + Enter

This will auto import the needed class.

Mbuso Kotobe
  • 50
  • 2
  • 7
-2

Use control+option+O to auto-import the package or auto remove unused packages on MacOS

Jun
  • 103
  • 6