Monday, October 4, 2010

How to List all/specified files in all jar's recursively in a directory

To list contents of a all jar files in current directory in below format
./wlserver_10.0/server/lib/api.jar : javax/net/ssl/impl/SSLSocketFactoryImpl.class

expandjars.sh
#!/bin/bash
JAR_LIST=$(find . -type f -name "*.jar") # find all files with .jar as extension.
for file in ${JAR_LIST}
do
     CLASS_LIST=$(jar -tvf ${file} | grep -i '.properties\|.java\|.xml\|.class' | sed 's/^.* //g')
     for class in ${CLASS_LIST}
    do
          echo "${file} : ${class}"
    done
done

If you are using Cygwin. After copying the code use dos2unix to avoid " syntax error near unexpected token"
dos2unix expandjars.sh

Explanation:
find . -type f -name "*.jar" - will find all the jar files in the current directory.
Store the output of above command in JAR_LIST.
Using "for" iterate for every entry (jar) in JAR_LIST
jar -tvf will unjar. Using grep find the files which you need.
echo them to stdout.

./expandjars.sh > classes.txt
once u get classes.txt
use grep to find the respective class like 'grep -i classname classes.txt'

No comments:

Post a Comment