My application is built on Java EE.
I have approximately 50 jars in this application.
Is it possible to search for a particular keyword (actually I want to s
One-liner solution that prints file names for which the search string is found, it doesn't jam your console with unnecessary "searching in" logs::
find libdir -wholename "*.jar" | xargs --replace={} bash -c 'zipgrep "BEGIN REQUEST" {} &>/dev/null; [ $? -eq 0 ] && echo "{}";'
Edit:: Removing unnecessary if statement, and using -name instead of -wholename (actually, I used wholename, but it depends on your scenario and preferences)::
find libdir -name "*.jar" | xargs --replace={} bash -c 'zipgrep "BEGIN REQUEST" {} &>/dev/null && echo "{}";'
You can also use sh instead of bash. One last thing, --replace={} is just equivalent to -I{} (I usually use long option formats, to avoid having to go into the manual again later).
Found the script below on alvinalexander.com. It is simple but useful for searching through all jar files in the current directory
#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done
Replace "codehaus..." with your query, i.e. a class name.
Sample output:
$ ./searchjars.sh
Looking in ./activation-1.1.jar ...
Looking in ./commons-beanutils-1.7.0.jar ...
Looking in ./commons-codec-1.3.jar ...
Looking in ./commons-pool.jar ...
Looking in ./jaxen-1.1-beta-9.jar ...
Looking in ./jdom-1.0.jar ...
Looking in ./mail-1.4.jar ...
Looking in ./xbean-2.2.0.jar ...
Looking in ./xbean-spring-2.8.jar ...
Looking in ./xfire-aegis-1.2.6.jar ...
Looking in ./xfire-annotations-1.2.6.jar ...
Looking in ./xfire-core-1.2.6.jar ...
Looking in ./xfire-java5-1.2.6.jar ...
Looking in ./xfire-jaxws-1.2.6.jar ...
Looking in ./xfire-jsr181-api-1.0-M1.jar ...
Looking in ./xfire-spring-1.2.6.jar ...
==> Found "codehaus/xfire/spring" in ./xfire-spring-1.2.6.jar
Looking in ./XmlSchema-1.1.jar ...
You can use zipgrep on Linux or OSX:
zipgrep "BEGIN REQUEST" file.jar
If you wish to search a number of jars, do
find libdir -name "*.jar" -exec zipgrep "BEGIN REQUEST" '{}' \;
where libdir
is a directory containing all jars. The command will recursively search subdirectories too.
For windows, you can download cygwin and install zipgrep under it: http://www.cygwin.com/
Edit 1
To view the name of the file that the expression was found you could do,
find libdir -name "*.jar" | xargs -I{} sh -c 'echo searching in "{}"; zipgrep "BEGIN REQUEST" {}'
Although there are ways of doing it using a decomplier or eclipse , but it gets tricky when those jars are not part of your project , or its particularly painful when using decompiler and you have 100s or 1000s of jars placed in several folders.
I found this CMD command useful , which helps in finding the class names in list of jars present in directory .
forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr "classname" && echo @path
You can either navigate to your desired path , and open cmd from there and run this command OR give the path directly in command itself , like this
forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"classname" && echo @path
My use case was to find a particular class in Glassfish , so command will look something like this :
Using jfind jar
JFind can find a Java class file anywhere on the filesystem, even if it is hidden many levels deep in a jar within an ear within a zip!
http://jfind.sourceforge.net/
Fastjar - very old, but fit your needs. Fastjar contains tool called jargrep (or grepjar). Used the same way as grep:
> locate .jar | grep hibernate | xargs grepjar -n 'objectToSQLString'
org/hibernate/type/EnumType.class:646:objectToSQLString
org/hibernate/sql/Update.class:576:objectToSQLString
org/hibernate/sql/Insert.class:410:objectToSQLString
org/hibernate/usertype/EnhancedUserType.class:22:objectToSQLString
org/hibernate/persister/entity/SingleTableEntityPersister.class:2713:objectToSQLString
org/hibernate/hql/classic/WhereParser.class:1910:objectToSQLString
org/hibernate/hql/ast/tree/JavaConstantNode.class:344:objectToSQLString
org/hibernate/hql/ast/tree/BooleanLiteralNode.class:240:objectToSQLString
org/hibernate/hql/ast/util/LiteralProcessor.class:1363:objectToSQLString
org/hibernate/type/BigIntegerType.class:114:objectToSQLString
org/hibernate/type/ShortType.class:189:objectToSQLString
org/hibernate/type/TimeType.class:307:objectToSQLString
org/hibernate/type/CharacterType.class:210:objectToSQLString
org/hibernate/type/BooleanType.class:180:objectToSQLString
org/hibernate/type/StringType.class:166:objectToSQLString
org/hibernate/type/NumericBooleanType.class:128:objectToSQLString
org/hibernate/type/CustomType.class:543:objectToSQLString
org/hibernate/type/TimeZoneType.class:204:objectToSQLString
org/hibernate/type/DateType.class:343:objectToSQLString
org/hibernate/type/LiteralType.class:18:objectToSQLString
org/hibernate/type/ByteType.class:189:objectToSQLString
org/hibernate/type/LocaleType.class:259:objectToSQLString
org/hibernate/type/CharBooleanType.class:171:objectToSQLString
org/hibernate/type/TimestampType.class:409:objectToSQLString
org/hibernate/type/CurrencyType.class:256:objectToSQLString
org/hibernate/type/AbstractCharArrayType.class:219:objectToSQLString
org/hibernate/type/FloatType.class:177:objectToSQLString
org/hibernate/type/DoubleType.class:173:objectToSQLString
org/hibernate/type/LongType.class:223:objectToSQLString
org/hibernate/type/IntegerType.class:188:objectToSQLString