Software Development

How to Remove all Unused imports in a Java file – Eclipse Shortcut

Eclipse IDE gives warning “The import XXX is never used” whenever it detects unused import in a Java source file and shows a yellow underline. Though unused import in Java file does not create any harm, it’s unnecessary increase length and size of Java source file and if you have too many unused imports in your Java source file, those yellow underline and Eclipse warning affect readability and working. In my last post on Eclipse, we have seen some Java debugging tips on Eclipse and in this post, we will see Eclipse shortcut to remove all unused imports in Eclipse. There are many options to tackle this problem e.g. you can collapse import section of code in Eclipse or you can altogether remove all unused imports from Java file, and we’ll see them in this short tutorial.

Anyway, let’s see How to remove all unused imports from Java file in Eclipse IDE.

How to remove all unused imports from Java file in Eclipse

Eclipse Shortcut
Yellow underline on above pic denotes all unused imports in a Java Source file

Here is a couple of ways to remove all unused imports from Java in Eclipse IDE :

1) Go to the line of unused import, press Ctrl + 1 which is an Eclipse shortcut of a quick fix, this will show a drop-down menu to fix this error and one of them would be “remove unused imports” and it will remove that import statement from Java file.

2) Above option is not very efficient if you want to remove multiple unused imports in single click because it removes them one by one. It’s better to use the “Organize Imports” feature of Eclipse IDE to remove multiple or all unused imports statement. For using this from Menu, Select Source–> Organize Imports. This will remove all unused imports from that Java file.

3) Third and my preferred option to remove all unused import statement from Java file is ctrl + shift + O, which is a shortcut of Organized import in Eclipse and you can say Eclipse shortcut to remove all unused import statement from Java file.

This will also work with the previous option and remove all unused import lines from the source file.

Do you want a bonus Eclipse tips? If yes then here you go, you can save all keystrokes required to remove all unused imports from Java source file by using Eclipse save actions, which are actions Eclipse automatically perform when you save Java file.

Just check the option of Organized imports in saving action under Window–> Preferences-> Java–> Editor–> Save Actions. This saves heap lot of time while working in Eclipse and one of the reasons I love Eclipse IDE for Java programming.

You can also configure formatting and few other actions which will be taken by Eclipse automatically whenever you save your file.

One more advantage of using Organize import or shortcut ctrl + shift +O is that it imports all packages which are required by code, So it not only saves time while removing import statement but also helps to import required packages in Java.

It means if you copy paste some code and see lots of red line as an error due to non-imported packages, just use this Eclipse shortcut or save your source file if you have configured Eclipse save action to organize import.

That’s all on Eclipse shortcut of removing all unused import statement from Java source file in Eclipse IDE. It’s always good to learn more about IDE on which you are working e.g. Netbeans or Eclipse to improve productivity.

Beginners Eclipse Java IDE Training Course

Eclipse Debugging Techniques And Tricks

The Eclipse Guided Tour – Part 1 and 2

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any questions or feedback then please drop a comment.

P. S. – Since Eclipse is one of the popular Java IDE and many Java programmer, who like to do things fast loves to learn new shortcuts. If you also like to learn new shortcuts then I suggest you check Top 30 Eclipse keyboard shortcut for Java programmer.  This is one of the many Eclipse shortcuts which we have discussed in that post.

Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: How to Remove all Unused imports in a Java file – Eclipse Shortcut

Opinions expressed by Java Code Geeks contributors are their own.

Javin Paul

I have been working in Java, FIX Tutorial and Tibco RV messaging technology from past 7 years. I am interested in writing and meeting people, reading and learning about new subjects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button