Thursday, 21 June 2018

How to remove all unused imports from Java File in Eclipse?

While writing any program in Eclipse IDE, we have seen many a times Eclipse shows an unwanted warning "The import xxx.xxx is never used" for unused imports.

Though the unused imports does not create any harm, it's just unnecessary increase in length and size of Java file.

In this blog post we will see how to remove these unused imports from Java file in Eclipse IDE.

1. First, Go to line of unused import, press CTRL + 1, this will show a drop down menu to fix this error and we can select "Remove unused import". This option will remove that particular unused import statement from Java File.

2.Second, as the above option is not efficient in case of removing multiple unused imports from Java file. In case of removing multiple or all unused import statements from Java file, we can select "Organize imports" from the drop down menu.

This option can be found by -
(a) Press CTRL + 1 and select "Organize imports"
(b) Right Click on Java File -> Click Source -> Organize Imports

3. Third, to remove all unused imports from Java file, we can use CTRL + SHIFT + O, which is a shortcut for "Organize imports"

One advantage of using Organize imports or CTRL + SHIFT + O is that it imports all packages which are required by code.

No comments:

Post a Comment