150

Wouldn't it be nice to just do a keystroke and have eclipse organize all imports in all java classes instead of just the one you are looking at? Is this possible? Is there a keystroke for it?

jjnguy
  • 136,852
  • 53
  • 295
  • 323
branchgabriel
  • 4,241
  • 4
  • 34
  • 48

6 Answers6

282

Select the project in the package explorer and press Ctrl + Shift + O (same keystroke as the single class version). Should work for packages, etc.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Todd
  • 30,472
  • 11
  • 81
  • 89
  • 7
    Love it, doesn't work when they're two classes with same name to import though "needs user interaction". – Blundell Aug 24 '11 at 08:22
  • 1
    @Blundell: Actually I found a workaround. I needed to correct the R class file for android, and the ambiguity is between com.example.R and android.R. The solution I found is to hide the android.R class in the build path configuration during the import correction, and then to put it back. It works since I never need to import android.R – njzk2 May 04 '12 at 14:02
  • Should select the src/main/java root package and CTRL+SHIFT+O does something for me, selecting the project does not. – Daniel Hári Feb 07 '18 at 13:47
  • Nice, works when select the whole project, but not work for nested sub packages when select a package. – Eric Feb 27 '18 at 03:57
22

You can edit the clean up options on save to make it organize imports. That way all of your imports will always be organized.

In eclipse 3.4 just go into Window - Preferences. In the tree view look under Java -- Editor -- Save Actions.

This is how I keep my imports organized all of the time.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
4

For Eclipse Helios, open the "Project Explorer" view, clic right on your src folder, go under the source menu, and clic "organize imports" that should work for all your project. or just hit the combination Ctrl + Shift + O on the src folder.

Jasper
  • 2,166
  • 4
  • 30
  • 50
0

If you want to run save actions against a whole project you can right click on the project, then choose Source and then Clean Up...

meh-uk
  • 2,031
  • 1
  • 23
  • 36
0

I have Leadbolt ads in my Android app. On every Leadbolt version, Leadbolt force me to re-update the name of the java import. For example, in my app it was :

import com.kwswsuyruzouggmtbzxb.* 

Then on the update they gave me a new package with the name:

import com.izwevyqazy.*

And therefore, I have to make a change from the older name to the newer name on around 20 or more of java source code files. So, what I did is to re-organize the imports using this way in Eclipse I.D.E.:

  1. Select in your application, the package name, the one directly under the folder "src" in your project folder.
  2. Right click the package name and select "Organize Imports"
  3. Please don't forget to change the names too in the AndroidManifest.xml; otherwise, your app will crash.
superlinux
  • 476
  • 1
  • 4
  • 12
0

For mac you can use Cmd + Shift + O. Alternatively, you can do it through the menu: Source -> Optimize Imports

You can run this on individual files, or for each source folder. The option is greyed out if you try it at the project level of the project explorer. So you may have to do it a couple time if you have a bunch of modules and source folders. :)

James Oravec
  • 19,579
  • 27
  • 94
  • 160