Skip to content

Grepping for classes in jars

There are more robust apps out there that find a Java class given a bunch of jars, but I mostly use this script. Usage is:


jargrep ClassName /scratch/a/dir/with/jars

Script:

#!/bin/bash
# usage: jargrep searchstring dir

for jar in `find $2 -name '*.jar' -type f`
do
  echo $jar
  jar tvf $jar | grep "$1"
done

I leave the echo of each jar searched in so I know how it's progressing, but it makes it hard to tell if the class was actually found when there are a lot of jars.

2 Comments

  1. msa wrote:

    … i'd use:

    jar tvf $jar | grep -q "$1" && echo $jar

    Monday, June 2, 2008 at 6:52 am | Permalink
  2. Phil wrote:

    Yes, that's a good option too. I like the "always" echo $jar so I know if I've accidentally set it on a huge NFS mount that will keep it busy for hours. One minor drawback of grep -q is that you find out if the classname is in the jar, but not at a glance what the FQN of the class is.

    Monday, June 2, 2008 at 7:19 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*