307

Does IntelliJ have an Organize Imports feature similar to that in Eclipse? What I have is a Java file with multiple classes missing their imports. Example:

package com.test;
public class Foo {
    public Map map;
    public JTable table;
}

In Eclipse I could use Organize Imports and it would automatically import both java.util.Map and javax.swing.JTable. In IntelliJ I have to individually go to each class, select it, then press Alt-Enter. There is an Optimize Imports feature but all it seems to do is sort and expand the existing imports.

I'm using IntelliJ 10.5.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
  • 20
    I still cannot find an alternative to eclipse's Ctrl+Shift+O – appbootup Mar 05 '13 at 15:38
  • 18
    Have you tried Ctrl + Alt + O? It automatically adds any unique imports and prompts you to choose between similar imports by pressing Alt + Enter. FYI, I'm using IntelliJ 12. – divesh premdeep Mar 08 '13 at 13:19
  • 6
    Except Ctrl + Alt + O won't return an import that it cannot decide on... For instance, if there are multiple imports to choose from (which I'm trying to figure out how to choose them, as I don't have the character symbol they suggest on my keyboard), then it will say, "Unused import not found". – Azurespot Apr 27 '15 at 07:30

16 Answers16

441

Ctrl + Alt + O (Code → Optimize Imports...) is what you're looking for, both on Windows/Linux and macOS keymaps.

It says "Optimize", but, if configured to do so, it will also:

  • organize existing imports
  • remove unneeded imports
  • add new required imports
  • add unambiguous imports on the fly

You can tune the auto-import settings under "Settings → Editor → General → Auto Import" as described by Dave.

You can also modify how the imports are auto-ordered under "Settings → Editor → Code Style → Java → Imports"

mcw
  • 3,500
  • 1
  • 31
  • 33
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 18
    I keep getting transitory downvotes, probably due to confusion. I've updated my answer to explain how this *does* satisfy the original question. – Ryan Stewart Jun 14 '13 at 00:54
  • 5
    It does not work when package-names in a dependency have changed. Even when it is still unambiguous I have to click the ClassName hit Alt+Enter and Enter, for each affected class... – Superole Nov 21 '13 at 12:12
  • 2
    I've had that problem. The easiest solution is to delete the bad line, and then the normal procedures will add a good line back. – Evan Reynolds Dec 04 '13 at 00:15
  • 7
    On Mac it is CTRL + ALT + O as well (at least in Android Studio 1.0) – jlapoutre Jan 05 '15 at 19:21
  • 10
    I find it quite stupid that the option "add unambiguous imports on the fly" is not checked by default. Who does prefer to make this by himself, class by class? – toni07 Feb 12 '15 at 10:51
  • It is not working as well as in eclipse. If you have a bad import, due to refactoring intellij will not delete it and will not add the good one. – sashok_bg May 12 '16 at 12:08
  • 3
    Should be `Settings → Editor → General → Auto Import`? – Steve Pitchers Mar 29 '17 at 19:35
  • 3
    Just a heads up, this answer is fine but the correct one should be, **Intellij does NOT have an auto import as powerful as Eclipse**. If you have ambiguous classnames to import you will not get an option dialog to select the correct one. – Lisandro Apr 04 '17 at 14:13
  • Although I agree, with @Lisandro I did find that turning on the option to automatically add unambiguous imports was interesting--it imports them as you type rather than waiting for you to optimize which, if I can get used to it, might be better than Eclipse. – Bill K Jan 17 '18 at 19:12
  • it doesn't work on package/folder/project level, does it? I did a 'ctrl + alt + o' on project, src, java and did mvn clean test ... i still got 'symbol not found' for recent refactor. – old-monk Jul 05 '18 at 17:53
  • On Mac OS -> *control* + *option* + *O* – Orlov Andrey Nov 24 '20 at 08:57
110

Under "Settings -> Editor -> General -> Auto Import" there are several options regarding automatic imports. Only unambiguous imports may be added automatically; this is one of the options.

Andrea Bergonzo
  • 3,983
  • 4
  • 19
  • 31
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Not enough up-votes for this answer. Yes, @ryan-stewart's answer fully answer's the OP's question, but this answer provides the solution to the question the OP didn't ask: how do I fix this forever, not just how do I fix it this one time. – Jonathan E. Landrum May 13 '16 at 20:27
  • 7
    It's now `Setting > Editor -> General > Auto Import` – Lucky Sep 13 '16 at 14:29
  • I fixed that adding the check on `classes` – Andrea Bergonzo Oct 29 '17 at 18:56
  • it doesn't work on package/folder/project level, does it? I did a 'ctrl + alt + o' on project, src, java and did mvn clean test ... i still got 'symbol not found' for recent refactor. – old-monk Jul 05 '18 at 17:52
  • @old-monk It works with what the editor knows about; it's more likely something in the refactor is broken, but impossible to say. – Dave Newton Jul 05 '18 at 18:47
50

Simple & short solution worked for me.

Go to File -> Settings -> Editor -> Auto Import -> Java (left panel) and make the below things: Select check box for "Add unambigious imports on the fly" and "Optimize imports on the fly"

Refer this.

enter image description here

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
  • it doesn't work on package/folder/project level, does it? I did a 'ctrl + alt + o' on project, src, java and did mvn clean test ... i still got 'symbol not found' for recent refactor. – old-monk Jul 05 '18 at 17:52
46

In addition to Optimize Imports and Auto Import, which were pointed out by @dave-newton and @ryan-stewart in earlier answers, go to:

  • IDEA <= 13: File menu > Settings > Code Style > Java > Imports
  • IDEA >= 14: File menu > Settings > Editor > Code Style > Java > Imports (thanks to @mathias-bader for the hint!) Settings dialog

There you can fine tune the grouping and order or imports, "Class count to use import with '*'", etc.

Note:
since IDEA 13 you can configure the project default settings from the IDEA "start page": Configure > Project defaults > Settings > .... Then every new project will have those default settings: enter image description here

t0r0X
  • 4,212
  • 1
  • 38
  • 34
45

July 2015 - I have concluded that IntelliJ does not support automatically resolving imports with a single function. "Organize imports" simply removes unused imports, it does not resolve unimported types. Control-Space resolves a single unimported type. There does not exist a single action to resolve all types' imports.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
  • 13
    This has been my experience. Frustrating. – ayahuasca Jan 07 '16 at 16:00
  • 1
    This also tripped me up. See @rohit's solution below. File -> Settings -> Editor -> General -> Auto Import -> Java and check all three options that he has highlighted. This worked for me. – Carl Rossman Mar 24 '16 at 16:34
  • 1
    Odd that this is the accepted answer, as it's highly inaccurate. See http://stackoverflow.com/a/8609200/839646. – Ryan Stewart Dec 06 '16 at 06:48
  • 2
    @RyanStewart it doesn't resolve ambiguous imports the same way Eclipse does. Eclipse will prompt when it's ambiguous, IntelliJ just ignore it. – Steve Kuo Apr 12 '17 at 18:21
  • I had the sam frustration, however, @Ryan is correct. `CTRL ALT O` , can add the missing imports. But you have to configure it in Setting > Editor -> General > Auto Import, and check "Add unambigous imports on the fly" Then resolves all imports in a class. – razvang Sep 02 '21 at 06:51
9

navigate to the file where you want to organize imports or just click on the whole package or even click on the project than press CTRL + ALT + O

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • 2
    [This answer already exists](http://stackoverflow.com/a/8609200/363701). You *could* consider adding the extra info ("you can click on the whole package or even click on the project than press CTRL + ALT + O") as a comment to that answer – Zach Lysobey Aug 07 '13 at 18:59
9

In IntelliJ 14, the path to the settings for Auto Import has changed. The path is

IntelliJ IDEA->Preferences->Editor->General->Auto Import

then follow the instructions above, clicking Add unambiguous imports on the fly

I can't imagine why this wouldn't be set by default.

Joe
  • 1,014
  • 1
  • 13
  • 28
7

Shortcut for the Mac: (ctrl + opt + o)

Tiago Gaspar
  • 253
  • 3
  • 5
3

Goto Help -> Find Action (Short Cut for this is Cntl + Shift + A) and type Optimize imports (Short cut for this is Cntl + Alt + O)

Manga Rao
  • 86
  • 4
2

Just move your mouse over the missing view and hit keys on windows ALT + ENTER

Shairyar
  • 3,268
  • 7
  • 46
  • 86
2

ALT+ENTER was far from eclipse habit ,in IDEA for me mouse over did not work , so in setting>IDESetting>Keymap>Show intention actions and quick-fixes I changed it to mouse left click , It did not support mouse over! but mouse left click was OK and closest to my intention.

Ali.Mojtahed
  • 2,537
  • 1
  • 15
  • 23
2

That plugin will automatically do the "organize import" action on file save: https://github.com/dubreuia/intellij-plugin-save-actions.

To install: "File > Settings > Plugins > Browse repositories... > Search 'Save Actions' > Category 'Code tools'". Then activate the "organize import" save action.

Alexandre DuBreuil
  • 5,431
  • 2
  • 19
  • 17
  • Judging a tool on one (missing) feature might not be considered as a thoughtful judgment. If the plugin do not work, do not hesitate to fill a bug there: https://github.com/dubreuia/intellij-plugin-save-actions/issues. – Alexandre DuBreuil Jun 04 '15 at 13:45
  • 6
    You are right, it is not a thoughtful judgment in normal situations. It is - however - a vented frustration on a stunningly missing basic feature from an ultimate tool. I spent 2 hours in agony trying to get this simple, essential feature to work, without success. And what made it worse, rather than been a built in feature by default (like how elegantly Eclipse does it) a plugin really needed to be written to achieve that! Its like building the a state of the art and most complex jumbo jet but without air-conditioning, instead, each passenger is given a paper fan! It took me 1 sec in Eclipse. – DhafirNz Jun 04 '15 at 19:03
  • 1
    Actually, using this plug-in highlighted a problem and weakness in IntelliJ. I had to disable this plug-in because it hijacks IntelliJ on every auto-save, rendering it unusable and slow. This is because it tries to do all the save actions every I type something. If auto save can be turned off somehow then this plug-in will indeed be very useful. – DhafirNz Jun 05 '15 at 00:00
2

I finally created a workaround around this frustrating issue. I'm not completely happy with the workaround, but it's better than nothing.

Basically, after you paste the source code and unambigous imports are fixed, just press F2 to highlight the next compiler error. If the current error is an import-missing error, press Alt+Enter, then Enter to select the Import option, then pick the correct import. Then, press F2 again.

Martin Vysny
  • 3,088
  • 28
  • 39
0

If you are missing just one import (the class name has red underline), click and hover the mouse over it, and a blue suggested import statement will appear. If you hit, Alt + Enter at this point, the import will be included in the file and the red underline should disappear.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Zorayr
  • 23,770
  • 8
  • 136
  • 129
0

Shortcut on Android Studio on MacOS: Control + Option + O

Cristan
  • 12,083
  • 7
  • 65
  • 69
Stigasaur
  • 86
  • 4
0

I did not have any wildcard * as mentioned in one of the answers, neither did any of the formatting through Android Studio mentioned worked

What helped was running this:

./gradlew ktlintFormat
Meenohara
  • 314
  • 4
  • 9