tags:

views:

184

answers:

7

Is there an easy way to rearrange imports in all *.java files in a project?

so for example:

import java.util.Calendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

would become

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

we have about 200 source files and they need to have imports in a order.

What would be the best way to approach this? I am open to a perl/python/groovy script that would do this...

+1  A: 

Eclipse has such an option out of the box (Ctrl+Shift+O). But it might not be easily automatable over a whole lot of files.

Joey
+1  A: 

I don't think either of them works perfectly, so make sure to review the limitations.

ykaganovich
+2  A: 

eclipse can help you organize imports for a project.

go to source/clean up.. and then use the configure button. this will bring up a filtered preferences dialog. click on enable project specific settings and then the edit button. change the profile name on top to something descriptive like 'organize imports' and then go to the code organizing tab. choose the organize imports checkbox. you can go through the rest of the tabs and deselect the other options. once you save all of that, go back out to the clean up... dialog and click finish.


edit:

if you are using the eclipse route, dont forget that you can order your imports (com before org,etc) if you'd like, as well as setting the 'Number of imports before needing .*' property.

akf
+1 You can also go to Java > Editor > Save Actions and have it do the import organization always, automatically, whenever a java source file is saved.
Carl Manaster
+3  A: 

In Eclipse, in the Package Explorer, right-click on the top-level package of the project. Choose Source > Organize Imports, and you're done, for all the files in your project.

Carl Manaster
+1 for brevity.
akf
You can also add "Organize Imports" to the list of "Save Actions", steps that Eclipse will take on every single file-save. Very useful - you never have to remember to hit the option again. I also like to run the formatter on every save (you can configure it to run on all lines of code or just the ones you edited).
matt b
+1  A: 

In Intellij IDEA you can set your preferences on Settings->Code Style->Imports and then optimize imports for the project by pointing to the directory in the project view and choosing Code->Optimize Imports.

To the cletus who asks why you want to do this, a look at imports can give you a first approximation of dependencies of a given class, especially regarding third party jars (you can pick up that they are used, and then follow their documented dependencies). Sometimes that is useful, so you may want to keep them organized. But in general I agree, it is not usually necessary to care.

Yishai
+2  A: 

Netbeans 6.5.1: Crtl+Shift+I

cam2574
A: 

Given that you actually asked this question, I'm assuming you're not using an IDE for your coding. If you're using vi, you can also use the built-in line sort feature to sort the imports alphabetically.

aberrant80