DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Grep Jars For Package Or Class

05.03.2005
| 10323 views |
  • submit to reddit
        Grep a bunch of jars for a certain package or class or expression:

find /path/to/directory -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q query && echo "{}"'

E.g.

find ~/soft/java/maven-repository -type f -name '*.jar' -print0 | xargs -n1 -0i sh -c 'jar tf "{}" | grep -q org.apache && echo "{}"'
    

Comments

Nicholas Sushkin replied on Thu, 2007/03/22 - 5:13pm

Here's my version of findClass shell script. Examples findClass java.lang.String findClass DataSourceRealm /usr/local/tomcat/{server,shared,common}/lib/*.jar #!/bin/bash class=`echo $1| tr . /` shift allDirs="$(echo "$@" $CLASSPATH| perl -F: -ane 'print join(" ",@F);')" printf -- "---- Searching for %s in %s\n" "$class" "$allDirs" for dir in $allDirs; do printf -- "---- Searching in %s\n" "$dir" case $dir in *.zip) unzip -v $dir | grep $class ;; *.jar) jar -tf $dir | grep $class ;; *) find $dir -print | grep $class ;; esac done