Script for finding a class inside a directory of JARs

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

Tags: , , , ,

2 Responses to “Script for finding a class inside a directory of JARs”

  1. Josh says:

    doesn’t http://javacio.us/ do this for you? Finds it in jars and in pom files (only for public repositories and jar files, though).

  2. Yup, javacio.us does something similar. But the reasoning for my script is that most of the time in my use-case, I have to search in private (company-internal) artifacts as well as commercials ones like BEA/Oracle JARs that would only exist in my HDD repo.