Is there any way to reorder imports in java source file

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

for example:

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

will 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 should have the import in order.

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

+4
source share
7 answers

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

+7
source

eclipse can help you organize the import for a project.

go to source / cleanup .. and then use the configure button. this will bring up a filtered preferences dialog. click enable project specific settings and then the edit button. change the profile name on the top to something descriptive, for example, "organize import", and then go to the code organizing tab. check the organize imports . You can go through the remaining tabs and deselect other options. as soon as you save all this, return to the clean up... dialog and click finish .


change

if you use the eclipse route, do not forget that you can order your import (com before org, etc.) if you want, and also set the value to "Import quantity before necessary. *".

+2
source

Netbeans 6.5.1: Crtl + Shift + I

+2
source

Eclipse has this feature out of the box (Ctrl + Shift + O). But it can be difficult to automate in many files.

+1
source

I don’t think that any of them works fine, so be sure to check out the limitations.

+1
source

In Intellij IDEA, you can set your preferences in Settings-> Code Style-> Import, and then optimize the import for the project by pointing to the directory in the project view and selecting "Code-> Optimize Import".

To cletus, who asks why you want to do this, a look at imports can give you a first approximation of the dependencies of a given class, especially with respect to third-party banks (you can choose what they are used and then follow their documented dependencies). This is sometimes useful, so you might want to organize them. But overall, I agree that you usually don’t need to take care.

+1
source

Given that you really asked this question, I assume that you are not using the IDE for your encoding. If you use vi, you can also use the built-in row sorting function to sort the import alphabetically.

+1
source

Source: https://habr.com/ru/post/1286385/


All Articles