IntelliJ Organize Imports - java

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.

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"

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.

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.

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!)
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:

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.

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

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.

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

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

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

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.

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.

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.

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.

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

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

Related

Eclipse doesn't suggest a class that needs to be imported later

I don't know if this is a bug or it's just how it's supposed to be، but whenever I try to type a class like the class random, in eclipse it doesn't give me a suggestion for it. Even when I press Ctrl + Space.
PS: However eclipse does tell me to import the class when I hover the mouse over it.
First thing to try is cleaning your projects via top menus: Project > Clean
note: pressing Ctrl+Space multiple times will cycle through different content assist proposals. Also Ctrl+1 will bring up the quick fixes dialog for errors on your current cursor's line.
I had a similar issue recently, go to:
Window > Preferences then Java > Editor > Content Assist > Advanced
In there I unchecked all the boxes and clicked Apply at the bottom. Then turned on the same options I had just disabled and clicked Apply again and that fixed it.

Why sysout won't work?

I checked the preferences settings in my Eclipse, it's all set to default with sysout option on, but when I typed sysout in eclipse, it won't automatically go into System.out.println().
I've checked several other related topics which mention ctrl + space.
It is a shortcut for input method on my computer. I don't know if this is related to my unable to use the sysout. If not, please let me know how I can get my sysout working.
If yes, please kindly let me know how I can reset 'ctrl + space' or set other shortcut for content assistant.
Eclipse > Preferences > Java > Editor > Content Assistant > Advanced
Make sure Template Proposals is checked in one of the shown lists.
In recent version of Mac (10.14.1) , Mac OS Settings --> Keyboard --> Shortcuts(tab) --> Input sources.
uncheck the setting ctrl +Space.
Now go to Eclipse IDE and it should work.
You have to press Ctrl + Space for the sysout (or equivalently: syso) shortcut to work in Eclipse, as sysout is not part of Java in anyway, on the contrary: it's an abbreviation introduced in Eclipse that only works after you press Ctrl + Space and expands to System.out.println().
By the way, syserr (or equivalently: syse) will expand to System.err.println() after pressing Ctrl + Space.
public static void main(String[] args)
This public static void ... blah blah has to be put for the sysout
to work
After trying all the answers above with no success I found another reason why Ctrl+Space could be prevented from working.
In my instance Ctrl+Space worked for some projects in the workspace but not others. I discovered that the project that it did not work for did not have the jdk in the build path, instead it had the jre for the application server (weblogic 12). The application ran fine on the server but Ctrl+Space to open the template proposals didn't work and other things like syntax highlighting were not quite right.
I hope this helps anyone who comes to this questions 3 years after it was asked (Like I did).
Eclipse Shortcuts:
Syso + Control + Space: Puts System.out.println()
Alt + Shift + R: Rename
Control + F11: Run
ALT + Up/Down: move the current line (or lines selected) in the editor up or down
Control + Shift + O: Import
Control + I: Indent
Control + D: Delete Line
Control + H: Search Your Entire Project
I had the same problem achieving simple ctrl+space templates with eclipse using jre. Showed empty templates for everything.
Try installing the jdk in separate directory then add into Preferences / Installed jre: add in the new directory. Then use that in your build path. It should solve the problem.
Is there anyway to modify the keyboard shortcuts in eclipse? here you can find out how to make a custom shortcut Window -> Preferences -> General -> Keys
edit: here is a video tutorial http://eclipseone.wordpress.com/2010/02/03/how-to-manage-keyboard-shortcuts-in-eclipse/
For me sysout in eclipse created in two lines. println() in new line..Then I found myself how to avoid this and get System.out.println in one line ..go to Windows > Preferences > Java->Templates (or type templates on search field in top left corner box).Then In template configuring section uncheck "use code formatter"..thats it.. :)
System.out
.println();
Seems like you in windows and your keyboard shortcut for eclipse is got replaced with the system keyboard shortcut for language. well, either you replace it the keyboard combination to shows the template proposals by :
1. open the preferences dialog
2. go to general -> keys
3. in the search dialog, find the command `Content Assist`
4. change the binding and then click OK button
or, you can change the combination key to change the language in the Text Services and Input Language and then go to the Advanced Key Settings tab and then change the key sequence.
Select in the menu bar "Window > Preferences > Java > Editor > Templates"
deselect at the lower end of window: "Use code formatter"
In my case it didn't work because ctrl+space was being used by another program Ubuntu(I-Bus) in my case ref. here Try changing ctrl-space by another key combination in general->keys to find out if this is causing the problem.
I started having the same problem with sysout shortcut when I installed the Scala plug-in in Enclipse. None of the answers worked, but the solution turned out to be very simple. I unchecked all Scala templates in Preferences -> Scala -> Templates So if you use any plug-ins, make sure their templates do not get in the way of your Java templates.
I was facing the same issue. If you use OS X Eclipse Ctrl+Space shortcut can be interfering with OS X system's "Selecting previous input source" shortcut using Ctrl+Space shortcut as default. OS X system shortcut has higher priority, that is the reason why Eclipse does not work.
Just go to System Preferencies/Keyboard/Shortcuts/Input Source and uncheck the "Selecting previous input source" or change the shortcut on something else. Eclipse should work after that even without restart.
I had the same problem. I had ctrl + Space used as a shortcut for input source in Mac. To disable this go to settings -> Keyboard -> Input Sources and uncheck Select the previous source. Than syso shortcut will work.
set your perspective on default and your problem will solve.
window -> perspective ->open perspective -> other -> select default

Remove unused import from All projects in eclipse

Is there a way , a shortcut or any setting which removes unused import from all projects ? effectively from workspace?
I found following question which states about removing from single file. How to remove unused imports from Eclipse.
But I need from whole workspace?
P.S.: Let me know if there are in any case ,a plug ins available for performing same.
Key combination Ctrl+ Shift + O - will trigger the Organize Imports that will remove and add missing imports.
To apply it on whole workspace you just have to select the project in the view Package Explorer and then use the key combination
Right click your working set node, or all projects that you need and select Source->Organize Imports.
Shortcut is Ctrl+Shift+O, it is basically written in the menu so if you are using a different OS, you should be able to find it. It works on single files as well as project nodes in the Package Explorer
As said, Ctrl+ Shift + O to do it manually or Window -> Preferences -> Java -> Editor -> Save Actions -> Organize Imports to have it organized automatically whenever you save a class.
Eclipse Workspace -> Java Resources -> right click on (src/main/java) folder -> Select Sorce from the menu -> Now finally select Organize import & you are done.
For macOS , you can use -->control+option+O combination.

How to configure Eclipse's "clean" to remove unused imports?

Due to a massive refactoring, i ended up having a lot of unused imports across many files. Using Eclipse, is there a way to remove them all?
I think there is no one shot solution through cleanup.
1) right click on package then Source->Organize Imports
2)`Ctrl+Shift+O` .
Here is Save Actions another way which can even avoid Ctrl+Shift+O
Select all your projects and then hit Ctrl+Shift+O. It's also available at Source -> Organize Imports if you right click on the selected projects.
You can right-click on source folder in the project and select Source > Organize Imports. This will organize the imports for (recursively) all source files contained in that source folder. This can also be done on individual packages.
As for tying the "Organize Imports" action to a "Clean", that will be trickier. I think the closest thing would be to attach an Ant build.xml to the "Clean" stage for the project. You could set up the build.xml to invoke Jalopy, which can organize imports. I don't recommend this approach, as it modifies files behind Eclipse's back, which can cause frustrating issues.
As a compromise, you could go to Window > Preferences > Java > Editor > Save Actions >
[x] Perform the selected actions on save
[x] Organize imports
Right click the package then select Source->Organize Imports.
Visit http://www.ibm.com/developerworks/library/os-eclipse-clean/ .
Detailed HOW TO work with profiles and clean up your code.
Its very simple Genius!
Import packages : Ctrl + Shift + O and
Remove Unused packages : Ctrl + Shift + O

Code cleanup in netbeans

Is there something similar to the Eclipse cleanup rules (Preferences > Java > Code Style > Clean Up) in NetBeans?
The cleanup rules in eclipse will allow you to clean things up like organizing imports, removing unnecessary casts, adding missing override annotations etc.
Also can you do that on a whole set of classes/packages instead of individual classes?
Refactor > Inspect and Transform
Is there something similar to the
Eclipse cleanup rules ((Preferences >
Java > Code Style > Clean Up) in
NetBeans?
In NetBeans 8.0, the powerful batch tool is Refactor > Inspect and Transform. See the tutorial page, Refactoring with Inspect and Transform in the NetBeans IDE Java Editor.
Individual tools are also available:
Fixing importsFor a single file, namespace, or project (depending on what's selected in Project window or has the focus): Source > Organize Imports (to sort and remove unused imports) or Source > Fix Imports (to sort, remove unused imports, and add missing imports). Or during every save: Tools > Options > Editor > On Save > Organize Imports.
Removal of trailing spacesFor a single file (place the carret in the code file): Source > Remove Trailing Spaces. Or during every save: Tools > Options > Editor > On Save > Remove Trailing Whitespace.
Code FormatFor a single file, namespace, or project: Source > Format. (Customize the rules in Tools > Editor > Formatting). There is also a plugin called Jindent you can install (I have not used it myself). Or during every save: Tools > Options > Editor > On Save > Reformat.
By default, Netbeans will display hint icons next to problematic lines of code and in the scrollbar, allowing you to perform an automatic fix if desired. These can be configured via Tools > Options > Editor > Hints. Netbeans can search all problems in the project using Source > Inspect. Or, to reiterate, many of these problems can be batch fixed with Refactor > Inspect and Transform.
The equivalent of Eclipse's "Code Cleanup" in NetBeans is "Format". It's under Source > Format, and the keyboard shortcut is Alt+Shift+F (on Windows). Unfortunately, unlike Eclipse, it doesn't seem like this can be configured in NetBeans.
For Netbeans use Ctrl+Shift+I to remove unused import from the file.
NetBeans 7.2 has Inspect & Transform refactoring to do this:
http://netbeans.org/kb/docs/java/editor-inspect-transform.html
tools -> options -> Tab "Editor" -> Tab "Hints" -> select Java (talking about) in JComboBox
and then you'd see yellow ocean ..., great and quick from ver 6.9
I know that netbeans points out these things automatically, but beyond merely pointing them out I don't know.
Right click on the project (or a package if you want), then choose "Inspect and Transform". In the dialog box, choose "Organize Imports" , from browse choose imports --> then --> Unused Imports
I don't know a way to do this en mass, but if you delete all imports from a source file and then right click in the source editor, you can select fix imports. This will import all the classes for you alphabetically, asking when it encounters package ambiguities.
As Soldier.moth pointed out, Netbeans will point out other issues, like casts and override annotations, in line, by use of a light bulb to the left of the source.
There is an Organize Imports plugin Read the DZone article for more info.
EDIT: I see there is a bug report to get this as part of the standard distribution.
If you just want to make your code have proper indentations on a whole project, simply click the project name, go to "Source," and then click "Format." Hope this helps!

Categories