Remove unused import from All projects in eclipse - java

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.

Related

Does Visual Studio Code have an auto-import feature for Java??

Does VS Code have an auto-import function or hot key for Java?
I installed Java Extension Pack but I found that I need to import every library manually.
For organize imports inside a java file
press SHIFT+ ALT + O
The vscode has also the option of enable auto organize imports on save action
Open the settings.json file (Ctrl+Shift+P than type settings.json) and add this:
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
when you save it will works :D
Kudos: https://www.youtube.com/watch?v=svCTgx6abFc
It is shift+option+o for vscode macos
A) in order to have everything working just like intellij or eclipse, there is an extension, install it, Ctrl+Shift+P ==> then write create java project.
B) now that you have the project scaffold right, just right click on the editor, choose source action, then organize imports and select the right libraries to be imported.

Organize imports in eclipse java

enter image description here
I'm working on a JUnit project on eclipse , please help me to organize imports ,because im using checkList plugin. I tried source +organize imports also , I tried Control+Shift +o , but both of them did not work . can you help .
To enable this, go to Windows -> Preferences -> Java -> Editor -> Save Actions and then enable Perform the selected action on save -> Organize imports. After this, whenever you save a java file, eclipse will remove the unused imports automatically.
Little googling might help
To fix this issue:
Go to Preferences -> General -> Keys Click on Filters... and de-select Filter uncategorized commands then Ok.
Then look for the command Go To Symbol in File and select it. Then click on Unbind and then Apply and Close
Ctrl+Shift+O. should now work.
Note:
Also unbind any other conflicting commands and leave only one command for Ctrl+Shift+O and set the When to Editing Java Source.

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

IntelliJ Organize Imports

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

How to import existing project into Workplace in Eclipse?

I am trying to follow Eclipse instructions to create a Hello World SWT application. I have the following instruction:
Import the SWT project from the main
menu via File > Import..., and select
Existing Projects into Workspace.
Specify the archive file you
downloaded and click Finish. This will
create the org.eclipse.swt project
which we will need to compile and run
the application.
I have troubles with that. In the "File" menu I see "Import...". When I click the "Import...", I do not see "Existing Project into Workspace". Instead of that I see "Select an import source:" after which I see a text field. After that I see a field with the following folders: General, CVS, Run/Debug, Tasks, Team, XML.
Can anybody help me with that, pleas?
You need to expand General.
If you expand the 'General' node (folder) you will see 'Existing projects into workspace' as an option. Select that and click on Next.
Eclipse has the wonderful search functionality on most of its important screens. Even if you don't know where exactly it is, you can type it and it appears. In this case type "Existing" into the text field above the list and you'll see what you are looking for.

Categories