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.

3 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
  3. Craft wrote:

    Seems like everyone has one of these scripts . . .

    Mine uses "unzip -l" instead of "jar tvf". I find unzip faster both in start up time and in processing time. Usually not noticeable until you query thousands upon thousands of the suckers.

    I also do """find $2 -name '*.[jwe]ar'""" to search J2EE artifacts as well.

    I also wrap the unzip | grep in an if/then statement. By default the clause just prints the file out, but one of the inputs to the script is another script which I use to modify the behavior a la ghetto closures.

    Thursday, July 22, 2010 at 4:21 pm | Permalink

Post a Comment

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