Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 736
Rob Audenaerde Avatar asked Nov 10 '11 14:11

Rob Audenaerde


People also ask

How do you know what jar to use?

Use –verbose:class flag with your java command line. This option enables loading and unloading of classes. It shows the jar file from which the class is loaded.


1 Answers

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)

like image 109
Thirler Avatar answered Sep 20 '22 03:09

Thirler