Tuesday, March 31, 2009

Tip: How to find to which jar a class file belongs

It's a common problem in larger projects, where you are using a Class reference, but you don't know which of the 20 jars in your project is actually providing the implementation.

In my case, I needed to know which jar file was providing the Hex class.

System.out.println(Hex.class.getProtectionDomain().
getCodeSource().getLocation().toString());

It turns out that it's in axis2.jar :)


1 comment:

Adam T. Bowen said...

As you probably know, a jar file is just a zip archive, so you could do something like:

ls *.jar | xargs -i unzip -t {} | grep Hex.class

presuming the Hex class is in a file called Hex.class There is also a zipgrep command, for looking for more obscure references inside zip archives.

(Nice one for all the great SageTV stuff)