Clean Imports V2.0.0
by Tom Brus    last change: 16-08-2005
 download now 

This ant tool cleans java import statements. You can download it freely. It comes without any guarantees (so backup your sources before using it). See the manual page for details on how to use it. The current version is V2.0.1. Feedback is welcome.

News Flash

No new version yet, unfortunately. I am working on JDK 1.5 compatibility, but my time is limited.

After withstanding them for a long time, I finally gave in: I have put ads on this page. If you start and keep clicking them I can justify to put more time into this project. So: click, click, click.

I have also put the sources in a sourceforge project: cleanimports. It is a rudimentary project, you have to get the sources from CVS. But they are there for you to inspect.

The Changes (V1.0.5 - V2.0.0)


• dependencies

Dependency on BCEL/jakarta-regexp removed. You do not need that jar anymore. The regexps are now JDK1.4 regexps.


JDK 1.4.0 and 1.4.1 compatible. JDK 1.4.2 is untested. JDK 1.3 is not supported anymore.

• bug fix

The old version had some problems with empty comments, they are fixed now.

• extra imports

You can now also show the imports from the package of the file itself and java.lang. This is not needed for the compiler but helps the reader.

• <options/>

I refactored the <collapse/> element into an <options/> and added some more options.

The Example

from this to this or this
import java.util.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
// util/io imports:
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
// xml imports: import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory;
// sax imports: import org.xml.sax.AttributeList; import org.xml.sax.DocumentHandler; import org.xml.sax.HandlerBase; import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;
// util/io imports:
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
// xml imports: import javax.xml.parsers.*; // sax imports: import org.xml.sax.*;

The Competition

I could not find the right tool to clean imports so I wrote my own. Here are my experiences with other tools:


• ImportScrubber

Has the 'small' limitation of not handling imports of final values correctly. Does not handle all inner classes correctly.

• Importifier

Has the same limitation of not handling imports of final values correctly.

The pros and cons

Here are some of the pros of CleanImports:
  • <javac> compatible <cleanimports> uses the same arguments and elements as the <javac> task. It needs a classpath and sources. So you can simply copy your <javac> task and rename it to <cleanimports> and if your code compiles it will be cleaned.
  • fully programmable format you have full control over the way the imports are output. imports can be subdivided into groups. Groups can be specified with a package prefix or with a regular expression (JDK1.4-regexp).
  • group comments group comments are only output when a group has members.
  • comments comments can be inserted anywhere
  • sorted per import group within each group the imports are sorted alphabetically.
  • collapsible imports from the same package can be collapsed (with "import apackage.*") when there are more then a certain number. This number can be specified per group.
  • ambiguity indicators when an import is ambiguous with respect to another class with the same name from another package from which imports are done, they are flagged with a warning comment. The comment states the classes with which it is ambiguous.
I came up with these cons, mail me if you find more:
  • sources must be compilable eh, not a real con I think.
  • does not run inside netbeans I don't know why but it is some class loader problem

Download and usage

You can freely download the jar and use it. To use CleanImports you'll need to download ant (of course)

  • CleanImports
contains the manual, a jar with the classes, an example dir and some TODO/History/Licence files.

Download the zip. Unpack it. Try to get the example to run on your machine.


You can use this software under the Apache Software License.

The Future

There are still some things I want to improve in CleanImports. Currently JDK 1.5 compatability has the highest priority. I would also like to implement a scheme to do automatic checkouts and checkins to version control systems. Then you can write a daemon that will automatically do import cleaning in your VCS.

Oh, and clicking those ads will raise the likelyhood of the future becoming reality .