Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default imports from sun instead of java package in intelliJ

My current system(MacOS 10.10.5) configuration is :

IntelliJ IDEA 2017.2.2
#Built on 9August,2017

java version "9"
Java(TM) SE Runtime Environment (build 9+181)

Every time I start working on a project(currently 1.8.0_65) and write a class using

List<String> example = new ArrayList<>(); 

=> If I click on the auto suggested tip for List => Import class

The default import statement introduced is

import com.sun.tools.javac.util.List;

while I expect it to be :

import java.util.List;

Is there any configuration that I can redress or is this some bug in the combinations I am using? Would want to know what settings has given preference to sun package over the java package?

Note : Have gone through the answer of Any way (or shortcut) to auto import the classes in IntelliJ IDEA like in Eclipse? but the options that I get when I type List doesn't include the one from java.util in my case.

Edit : The import for java.util.List is not missing, I am able to import the same manually.

like image 324
Naman Avatar asked Sep 02 '17 10:09

Naman


People also ask

How do I automatically import imports in IntelliJ?

Automatically add import statements You can configure the IDE to automatically add import statements if there are no options to choose from. In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Select the Add unambiguous imports on the fly checkbox, and apply the changes.

How do I change import settings in IntelliJ?

Call File | Manage IDE Settings | Import Settings from the main menu. Select the ZIP archive that contains your settings in the dialog that opens. Select the settings you want to apply in the Select Components to Import dialog that opens and click OK.

How do I turn off auto import in IntelliJ?

You can disable auto-import on completion and use quick-fixes instead: In the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General | Auto Import. On the Auto Import page that opens, use the checkboxes in the TypeScript/JavaScript area to enable or disable import generation on code completion.

How do I manage imports in IntelliJ?

In Eclipse, you press CTRL + SHIFT + O “Organize Imports” to import packages automatically. For IntelliJ IDEA, if you press CTRL + ALT + O “Optimize Imports”, it just removes some unused imports, never imports any package.


2 Answers

I had an issue like this with java.util.Arrays not showing up in completion lists. Turns out I had somehow added it to the import and completion exclusion list. My finger must have slipped in a quick action popup at some point.

Maybe the same thing happened to you. Open your Settings dialog and find your way here:

Settings > Editor > General > Auto Import

Then, look for this list of Excluded imports:

Exclude from Import and Completion

See if java.util.List shows up in that list, and if so, remove it.

With java.util.List excluded, com.sun.tools.javac.util.List might be the only other List type in your class path. If you have the "Add unambiguous imports on the fly" option enabled, IntelliJ would then import the sun class without even asking.

like image 69
Mike Strobel Avatar answered Sep 19 '22 14:09

Mike Strobel


I don't think there is such option to prefer a package over another for importing classes.

Instead, you could exclude com.sun.tools.javac.util.List from auto import. To do so, in Class to Import window, click on the arrow on the right, then Exclude com.sun... from auto-import: exclude auto-import popup

After that, java.util.List should be the first choice of import. If later you need to remove some excluded imports, you can go to PreferencesEditorGeneralAuto Import to find them.

like image 27
Jonathan Avatar answered Sep 19 '22 14:09

Jonathan