Thursday, 21 August 2008

jar search tool

How often do you want to search a directory structure to see which jar a particular class is in.

I always end up doing the following:

list=`find . -name "*jar"`
for file in $list
do
echo $file
jar tvf $file grep Somepattern
done


I have done this long hand so many times that I have decided to script it.
Use this script jar_search like this.
jar_search ObjectPool
or
jar_search ObjectPool comm

The second parameter is optional and allows you to restrict what jars you will search through.

Here is the jar_search script:

#
# search a list of jar files for a pattern
# the jar pattern is optional
# By Bill Comer, August 2008
#

if [ $# -lt 1 ]
then
echo "Usage: jar_search [searchPattern] [jarPattern]"
exit -1
fi

searchPattern=$1
jarpattern=$2

list=`find . -name "*$jarpattern*.jar"`

for file in $list
do
found=`jar tvf $file | grep $searchPattern`
if [ ! -z "$found" ]
then
echo $file
echo $found
fi
done

3 comments:

Anonymous said...

if you want to search for your jar files in a large online database you might also find

www.findjar.com

helpful. it contains the entire maven repository and much more. hope it helps.

Luke said...

The maven repository search engine at jarvana.com is another great online tool for finding the jar files containing particular classes. It currently searches about 8 million classes.

Bill Comer said...

Apologies but have just noticed that the pipe '|' was missing from the line:

found=`jar tvf $file | grep $searchPattern`

rather critical but it is there now.