Eclipse Java IDE has a shortcut Ctrl+Shift+O to automatically add unused imports. Where I can find script (bash, python or something other that can be executed via shell) to do this IDE-agnostically, for example, in text editor that can use scripted external tools like gedit?
Asked
Active
Viewed 175 times
-4
-
IntelliJ adds them in dynamically without a shortcut or a script. Get a better IDE. – duffymo Sep 10 '16 at 11:33
-
You don’t want it to be too automatic. There are more than one class called `Date` and more than one `List`. Getting the wrong one imported will give you more pain than help. – Ole V.V. Sep 10 '16 at 12:24
-
1@OleV.V. I know, so I want to give a prompt every time there's a conflict. – Fireburn Sep 10 '16 at 13:03
-
@duffymo I said that I wanted the solution to be IDE-agnostic, i. e. usable **without** any IDE. – Fireburn Sep 10 '16 at 13:17
-
I think that's silly. – duffymo Sep 10 '16 at 13:17
-
@duffymo That's not silly, that's minimalistic. Current IDEs are bloated with features that's not needed for me. I just want to use some scripts for features I **will** use. And I don't want to use IDE, because I often work in environments where use of IDE is impossible. – Fireburn Sep 10 '16 at 13:19
-
Nonsense. By all means go ahead and write that script. I'll use IntelliJ and get things done. Your requirement is....wrongheaded in my view. But please persist. I don't care. – duffymo Sep 10 '16 at 13:21
-
@duffymo If you don't care, why were you commenting here in the first place? Don't know the answer that _will_ suffice for a question - just skip it and go further. Isn't that how StackExchange works? – Fireburn Sep 10 '16 at 13:23
-
I know how SO works. I'm commenting to save others from views like yours. – duffymo Sep 10 '16 at 13:24
1 Answers
-1
Make a script that parses in the classes inside the .jar files located in your Java installation, taking in account the directory name and class name to determine the fully qualified name.
For instance, java.lang.String class is located in <jre installation>\lib\rt.jar
.
If you reference multiple classes called String
, get all fully qualified names with class name String
, and simply handle some shell input to determine which package should be used. If there is only one with that name, import it if it hasn't been already.
Should be simple enough in any language that can handle iterating files in JARs.

Alexander Henne
- 84
- 1
- 6
-
So just run through the whole java lib dir and the classpath xml file and find a file in one of the jars. Thank you for the answer! – Fireburn Sep 10 '16 at 13:36