I have several simple classes in Java IntelliJ code, however soon as I finish adding any import statement that statement disappears. It let me type statement import java.util.* , but when I complete writing statement with ; in that moment whole statement disappears, with no error displayed.
This is probably because Settings -> Editor -> General -> Auto Import -> Optimize imports on the fly is enabled. IDEA finds an unnecessary import and removes it. So you should deselect it if you want to disable this behavior. However, I recommend not to organize imports manually. Just write your code and IDEA will import everything for you.
Adding Wild Card Imports import com.package.* is generally considered bad practice when writing Java as it clutters the local namespace that you're developing with. In this case,
Intellij recommends wildcard imports if you're using more than 5 classes from the same package in your current namespace, however that can be disabled too.
Source: here
All import statements were disappearing. In Setting>Editor>General>Auto Import I disabled Optimize imports on the fly option and import statements dont disappear anymore.
For the newer versions of Intellij, press Ctrl + Alt + Shift + s to open project settings, then choose Modules, under Dependencies tab choose the green + and add the folder in which your . class file is located. See if that helps
Related
I've never had this problem before but check it out:
All of a sudden, IntelliJ tells me it wants to import these classes from org.springframemwork instead of my own.
Problem imports:
ViewNames, Mappings, AttributeNames
Example:
IntelliJ wants to import this
org.thymeleaf.engine.AttributeNames;
instead of my AttributeNames!
My solution:
File | "Invalidate Caches / Restart"
IntelliJ began getting an image of a non-existent directory structure and needs to be refreshed
Sometimes IntelliJ is simply "too quick" and adds an import for some obscure class. Happens to me from time to time, too.
The easiest solution: remove the wrong import, and in "organize imports" gets it wrong again, press option-enter. That suggests "import class", and typically gives you a list of choices to choose from, in case the class name is ambiguous.
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.
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:
Then, look for this list of Excluded imports:
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.
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:
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 Preferences › Editor › General › Auto Import to find them.
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
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:
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.
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.
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.
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.
Can't import all at once but can use following combination:
ALT + Enter --> Show intention actions and quick-fixes.
F2 --> Next highlighted error.
Seems like IntelliJ IDEA will import missed class automatically, and you can import them by hit Alt + Enter manually.
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.
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
3- Now press Ctrl + Shift + O. You are done!
Use control+option+L to auto import the package and auto remove unused packages on Mac
Use Alt+Enter for importing a single package or use Alt+Shift+Enter to auto import all the unambiguous packages in the current file.
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
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
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
Hover on top of the code which needs a class then press
alt + shift + Enter
This will auto import the needed class.
Use control+option+O to auto-import the package or auto remove unused packages on MacOS
In eclipse, I would write my code and at the end of it all, I would do CTRL+SHIFT+O and all my imports would be added and re-organized, etc.
I have already seen all the other answers to do CTRL+ALT+O (I changed this back to eclipse keymap) and I have already done all the settings possible. IntelliJ will just not ADD any new imports, I have to manually go to the broken import and press ALT+ENTER and then mouse over to import class, for EACH NEW CLASS. Sometimes, this doesn't happen and intelliJ will actually work by itself.
Is there really no way to force an auto add imports??
This is how my Auto Import settings look like:
If you are happy to reformat the code AND optimize the imports, you can try ctrlaltL (Reformat code).
Using that function you get a dialog box: specify the directory of files you want formated, and check "Optimize imports".
Okay, first the project I am working on has two packages, first is murach.business which contains invoiceCalculations.java Second is murach.forms which contain InvoiceForm.java(JFrame) and SwingValidator.java
So, I am supposed to add Add import statements for the murach.business.InvoiceCalculations and java.text.NumberFormat classes in Invoiceformjava that i created from the scratch.
But, I am getting errors when i code that. netbeans says unused code for the below code:
have i written the code incorrectly? whats wrong? Please push in right direction.
import murach.business.InvoiceCalculations;
import java.text.NumberFormat;
"Unused code" doesn't mean that you have a syntax error. It just means that the import is not necessary, because the current code of the class doesn't use the imported class yet. If you add code inside the class that uses the imported class, the warning will disappear.
Note that, with current IDEs, you usually don't bother adding import statements manually. You use the class without importing it, and use a keyboard shortcut (or autocompletion) to add the necessary imports (and remove the unused ones) automatically. In Netbeans, the command is "Fix Imports (Ctrl-Shift-I)"
These are not errors but warnings that Netbeans gives you. The code should still compile fine