Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, October 12, 2021

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

How to remove all unused imports in Eclipse
Eclipse IDE gives a 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 to increase the 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 the import section of code in Eclipse or you can altogether remove all unused imports from the Java file, and we'll see them in this short tutorial.

If you are completely new to Eclipse IDE, then I suggest you first go through a comprehensive course like The Eclipse Guided Tour - Part 1 and 2 from Pluralsight, which will teach you everything you need to know about Eclipse from a Java development point of view.

Btw, you will need a Pluralsight membership to access this course which costs around $29 per month or $299 annually, but it's completely worth it because Pluralsight is like Netflix for Software developers.

It has more than 5000+ courses on the latest technologies and since programmers need to keep themselves up-to-date, there is no better way than watching these courses from anywhere and anytime.

I mostly watch them while commuting. Last, but not least, Pluralsight also provides you a 10-day FREE trial without any obligation, which means you can access this course for free also.

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

How to remove all unused imports from Java file in Eclipse

How to remove all unused imports in Eclipse from Java 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 a 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 statements. For using this form Menu, Select Source--> Organize Imports. This will remove all unused imports from that Java file.

3) Third and preferred option to remove all unused import statements from the 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 statements from the Java file.

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

Eclipse Shortcut to Remove all Unused imports in a Java file

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

Just check the option of Organized imports in saving action under Window--> Preferences-> Java--> Editor--> Save Actions. This saves a 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 statements but also helps to import required packages in Java.

It means if you copy-paste some code and see lots of red lines 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 statements 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.

Other Java Eclipse articles you may like to explore
  • 30 Useful Eclipse Shortcuts for Java Developers (list)
  • How to remote debug Java applications in Eclipse? (tutorial)
  • 10 Eclipse debugging tips Java developers should know? (see here)
  • How to attach source code for the JAR file in Eclipse? (guide)
  • Eclipse shortcut to print System.out.println statements? (shortcut)
  • How to increase the console buffer size in Eclipse? (steps)
  • How to use spaces instead of tabs in Eclipse? (guide)
  • How to create an executable JAR file from Eclipse? (example)
  • 3 Books to Learn Eclipse IDE for Java developers (list)
  • How to Increase Heap Size of Java Program running in Eclipse? (guide)

Thanks for reading this article so far. If you like this Java Eclipse tutorial and tips then please share it 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 the Top 30 Eclipse keyboard shortcuts for Java programmers.  This is one of the many Eclipse shortcuts which we have discussed in that post.

No comments :

Post a Comment