Google+ How to remove unused imports from all class files ~ Java Geek Zone

Friday 23 October 2015

On 01:42 by Unknown in , , ,    No comments
Often we write the code and import the classes in the import section of the class file.

Code never stays unchanged for lifetime as change is the only constant thing in programming.

Many times, after code refactoring or bug fixed, Eclipse IDE will show a yellow underline for all unused imports at the top of your class and said “The import xxx is never used

When we change the code sometimes we forget to remove the old imported classes under import section. Keeping something which is never used in code is not considered a good practice.

Eclipse provides multiple options to get rid of those unused imports. I will explain all the options available in eclipse to achieve this.

Option-1

This option is not much useful as it only works for single line and not for all unused import in whole class.

  1. Open your class file
  2. Go to the line of unused import
  3. Press ctrl + 1 which is a eclipse shortcut of quick fix, this will display a drop down menu to fix this and you will see an option "remove unused imports"
  4. Click  on "remove unused imports"


Option-2

This option will remove all unused imports from whole class.

  1. Open your class file
  2. Right click on code area
  3. Go to “Source”
  4. Click on “Organize Imports”


Option-3


This option will also remove all unused imports from whole class.

  1. Open your class file
  2. Press Shift + Ctrl + O  for PC or Command + Shift + O  for Mac
Advance Options :

Option-4


This option will allow you to remove all unused imports when you save your class file

  1. Click on “Window”
  2. Click on “Preferences”
  3. Select & Expand “Java”
  4. Select & Expand “Editor”
  5. Click on “Save Actions”
  6. Check the option “Perform the selected actions on save”
  7. Check the option “Organize Imports”
  8. Click on “Apply” in Preference window
  9. Click on “OK” in Preference window


Above option also allows you to set preference for formatting source code and invoking other additional actions.

Option-5

This option will allow you to remove all unused imports from all class files of your package. It’s very nice and efficient option to achieve this in one go.

  1. Go to “Project Explorer”
  2. Right click on your package
  3. Go to “Source”
  4. Click on “Organize Imports”


Option-6

This option will allow you to remove all unused imports from all class files of your package/project. You can configure this setting in Eclipse [built-in] profile or you can create your own profile for the same. I will show you by creating your own profile and configuring it.

Configuration Actions:

  1. Click on “Window”
  2. Click on “Preferences”
  3. Select & Expand “Java”
  4. Select & Expand “Code Style”
  5. Click on “Clean Up”


  6. Click on “New”
  7. Enter the “Profile Name” of your choice
  8. Click on “OK”
  9. Go to Tab “Code Organizing”
  10. Check the option “Organize Imports”
  11. Click on “Apply” in profile window
  12. Click on “OK” in profile window
  13. Click on “Apply” in preference window
  14. Click on “OK” in preference window


Executing Actions:

  1. Go to “Project Explorer”
  2. Right click on your package
  3. Go to “Source”
  4. Click on “Clean Up”
  5. Choose a profile you configured.
  6. Click on “Next”
  7. Click on “Finish”




That is all about the options to remove unused imports from the class files.

I hope these options will make your life easier to work with eclipse and make code neat and cleaner.

I will come up with more eclipse tips soon till then happy coding… 

Cheers!!!

0 comments:

Post a Comment