Showing posts with label jar. Show all posts
Showing posts with label jar. Show all posts

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