Posts Tagged ‘Automation’

Script for finding a class inside a directory of JARs

Sunday, June 14th, 2009

In the spirit of automating anything I’ve done more than twice manually, here’s an incredibly simple yet useful little script to recursively search a tree of JARs for a class file. I most often use this against a local Maven repository.

#!/bin/sh

#Example Usages:
# findjars com/ambientideas/SuperWidget
# findjars AnotherWidget

CLASSNAMETOFIND="$1"

echo "Searching all JARs recursively..."
for eachjar in `find . -iname "*.jar"`
do
  #echo "Searching in $eachjar ..."
  jar tvf $eachjar | grep $CLASSNAMETOFIND > /dev/null
  if [ $? == 0 ]
  then
    echo "******* Located "$CLASSNAMETOFIND" in $eachjar *******"
  fi
done