How do I organize imports automatically in Eclipse?

JavaEclipse

Java Problem Overview


I find myself typing Ctrl+Shift+O several times to organize imports (usually imports which are no longer required, generating warnings).

Is there to make Eclipse do this every once in a while or, at least, every time I save a file?

Java Solutions


Solution 1 - Java

Preferences->Java->Editor->Save Actions->Configure...

You can configure the removal of unused imports when saving from the Unnecessary Code tab.

Adding imports would normally be done as you are writing the code otherwise it won't compile.

This is how it looks like: enter image description here

Solution 2 - Java

Doing while saving is great, but if you already have lots of files that need it applied to you can also select the package in the Package Explorer and go to Source->Organise Imports It will apply to each file below that package.

You may be doing this already, I just wanted to make it clear that it can be applied to many files as can Source->Format.

Solution 3 - Java

Either: <project>->Properties->Java Editor->Save Actions

Or

Windows->Preferences->Java->Editor->Save Actions

Solution 4 - Java

Yes, it is called Save Actions. There you can enable several thing should be done when saving a file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEclipseQuestionView Question on Stackoverflow
Solution 1 - JavaRobinView Answer on Stackoverflow
Solution 2 - JavawestonView Answer on Stackoverflow
Solution 3 - JavaPuceView Answer on Stackoverflow
Solution 4 - JavaFabian BarneyView Answer on Stackoverflow