Search code examples
javajarcode-coverage

Determine unused jars by code coverage?


I have a project with a million jars (well, a lot). They came to me by maven, and I only use a small set of functionality. For cleanness sake, I was wondering with jars I could do without.

My first thought was to run the program with a code-coverage tool, and then find the classes that are touched.

Has anyone done this before? Or are there smarter tricks to achieve the same?


Solution

  • You can run the project using the -verbose:class VM option. This will print for all loaded classes where they are loaded from. Using some smart parsing app/grep/regexp will allow you to filter the jar names into a set of unique entries and tell you which are used.

    I think this would be easier because it will automatically tell you if a class is used and if so in which jar.

    Of course the problem with this and code coverage is that it will be possible that you delete a jar that is only used in some exceptional case, but your compiler will complain if you deleted one or two too many, leaving you with the (mostly not too complicated) task of finding which jar the class is in.

    Possible suggestion when using linux:

    java -verbose:class <your startup command here> | grep "\[Loaded" | grep -o "from .*\]" | cut -c 6- | sort | uniq

    If you aren't using linux, then save to a file, get a linux machine and run on linux (or use something to run bash commands on windows)