Monday, March 10, 2008

WhichJar Utility

Where oh where is that class? Setting up your classpath in Java is half the battle. And finding in which jar the class you are looking for lives can be a tedious task. Measured in software years it was ions ago that my friend Toby introduced me to a little shell script he wrote called whichJar. I think I have been using it on a daily basis ever since. It may have changed a little over the years, but not too much, so here it goes:

#!/bin/bash
# nasty script to find a string in the contents of a jar file.
# probably a better way to do this, but then again...

if [ "$1foo" = "foo" ] ; then
echo "$0 usage: `basename $0` string"
echo "where string is the string to look for in the jar's contents"
exit 1
fi

for i in `find . -name \*.jar`
do className=`(jar tvf $i | grep $1)`;
if [ "$className" != "" ]; then
echo -e "$i\t$className";
fi
done

To use it save the above code of to a file called 'whichJar' in your bin directory, and make it executable
chmod a+x whichJar

Then, for example to find the class 'javax.jms.Topic' in any of the jars in the current directory, or any of its subdirectories, simply type
whichJar javax.jms.Topic

Thank you Toby.

No comments: