Wednesday, April 22, 2009

Search in Jars

Does one need a specialized application to search jar files for certain class or function? Just before recently I thought it was true. I found some applications like this on the Web and I decided to download some of them. Nevertheless, it turned out that all downloads were blocked in my office environment. But any coin has two sides, so it was opportunity for me to come up with simple pair of scripts that does the trick.

//---------------------------- file searchJars
#!/bin/bash
CACHE=~/bin/listOfJars_

if [ ! -f $CACHE ]
then
 cacheJars $CACHE
fi

for file in $(cat $CACHE) ; do
 found=$(jar tf $file | grep $1)
 if [ "$found = "" ]
 then
  continue
 fi
 echo ============= FOUND ============
 echo $file
 echo $found
done
//---------------------------- file searchJars end

//---------------------------- file cacheJars
#!/bin/bash
CACHE=~/bin/listOfJars_

if [ "$1" != "" ]
then
 CACHE=$1
fi

find ~ -name *.jar > $CACHE
//---------------------------- file cacheJars end

The scripts are just bare bones, not foolproof, they don't suggest help or prevent incorrect usage. You can add this stuff yourself.
This is what you do: create folder bin in your home directory, create files searchJars and cacheJars with above content there. Don't forget to make them executable with
 chmod +x <filename>
Then run
 ~/bin/searchJars SomeMethod

First time the execution will be slower because of caching all jars' into the list. Next time the step will bi omitted. To refresh the cache run
 ~/bin/cacheJars
without parameters.

No comments:

Post a Comment