Some time ago I’ve posted about using multiple -exec options for find command which can be used to find in what JARs particular class lies. Below you can find enriched and more intuitive/detailed solution for finding your class in a bunch of JARs:

#!/bin/bash

if [ $# -lt 1 ]
then
        echo 'Usage: jarfind CLASS_NAME [DIR_WITH_JARS]';
        exit 1;
fi

WHAT="$1"

if [ $# -eq 2 ]
then
        WHERE="$2"

else
        WHERE="."
fi

FIND_RESULT=`find "$WHERE" -maxdepth 2 -name '*.jar' -type f` 

for LINE in $FIND_RESULT 
do
        grep -q "$WHAT" "$LINE" 
        if [ $? -eq 0 ]
        then 
                echo "$LINE"
                jar tvf "$LINE" | grep "$WHAT"
                echo
        fi
done

The first argument is the class name you want to search for and the second one is the directory in which the jar files should be located (relative to the current directory). If the second argument is not specified, it defaults to current directory.
Result of this command should look something like this:

piotr@piotr-Vostro-3550:~/KonaKart_6.0/webapps$ jarfind AdminCustomer konakart/WEB-INF/lib/
konakart/WEB-INF/lib/konakartadmin.jar
11064 Tue Jan 24 21:16:16 CET 2012 com/konakartadmin/app/AdminCustomer.class
4008 Tue Jan 24 21:16:18 CET 2012 com/konakartadmin/app/AdminCustomerGroup.class
8166 Tue Jan 24 21:16:16 CET 2012 com/konakartadmin/app/AdminCustomerSearch.class
34964 Tue Jan 24 21:16:28 CET 2012 com/konakartadmin/bl/AdminCustomerMgr.class
2697 Tue Jan 24 21:16:28 CET 2012 com/konakartadmin/blif/AdminCustomerMgrIf.class
2389 Tue Jan 24 21:16:28 CET 2012 com/konakartadmin/blif/AdminCustomerTagMgrIf.class

If you save it as a BASH script file e.g. jarfind.sh and you set an alias for it, i.e. alias jarfind='/home/username/.jarfind.sh' in your .bashrc file than you can use it like this:

// Assuming that *.jar files are in the current directory
$ jarfind MyClassName

// Assuming that *.jar files are in the "myapp/WEB-INF/lib" directory,
// relative to the current directory
$ jarfind MyClassName myapp/WEB-INF/lib

Based on Andrew Monkhouse oneliner posted here. Much obliged for it Andrew!

Did you like this? Share it:

Leave a reply

required


*

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>