Tips & Tricks

Excluding Classes from Auto-Import

IntelliJ IDEA auto-import popup is a very handy feature that lets you stop worrying about import statements in your Java class files. However, sometimes it can show more choices than you need (e.g. when some of the class names in your project match the names of internal JDK or unrelated library classes).

To make auto-import easier to use and to avoid accidental imports of wrong classes, you can open the Settings dialog, then go to Editor | Auto-Import and add their package prefixes to the exclusion list. This will also hide the specified classes from class name completion lists.

Comments below can no longer be edited.

7 Responses to Excluding Classes from Auto-Import

  1. Avatar

    Joseph says:

    March 30, 2009

    This is nice but I think it’s not smart enough implemented (at least not for IntelliJ’s used level 🙂 ):
    – such a setting should be project/module dependent in first place.
    (when switching from one project to the other it is ugly to constantly edit those settings)
    – even more, IntelliJ could be smarter here: e.g. the facets could know what classes they usually don’t require, i.e. in a module when a certain facet is active, than it could automatically exclude from completion.

    Best example I can think of is to exclude the java.awt.* and javax.swing.* classes when having the webmodule active, since many webframeworks also have “TextArea” or “TextFields” , and intelliJ is proposing the AWT/Swing classes in this case.
    (Also in web modules users work allot with the List interface, and IntelliJ is constantly proposing java.awt.List 🙂 ).
    It would be also pretty easy to make a Facet/framework incompatibility/improbability matrix, to make IntelliJ behave smarter.

  2. Avatar

    Pat Niemeyer says:

    March 30, 2009

    Yay! java.awt.List is dead, long live java.util.List!

  3. Avatar

    Stephen Friedrich says:

    March 31, 2009

    I have agree with Joseph.
    Me myself I know not to import stuff from com.sun.*
    However I’d like to forbid that in the project settings.
    For example some colleagues often manage to import @NotNull from a strange com.sun.* package rather than using the idea annotation.

  4. Avatar

    eyrieowl says:

    April 7, 2009

    This must be the location in the more recent versions. Still on IDEA 7.0.4, and the similar setting is under IDE Settings -> Code Completion. Useful tip, though.

  5. Avatar

    Shadow says:

    September 30, 2011

    How do you turn this irritating thing off?!?!?

    It is seriously pissing me the f*&k off!!!

  6. Avatar

    Shadow says:

    September 30, 2011

    Oh thank god, I figured it out!

    Settings -> Editor -> Code Completion -> Preselect the first suggestion: -> Never

  7. Avatar

    Dawid Hyzy says:

    April 9, 2020

    Is there way to add this to project settings?

Discover more