We often need to search for java class file in given jar files to make sure that the class is present. Or sometimes it may happened that you reference the class in your java code but while compilation you don’t know which jar should be included in the compile time dependencies. There is also jarscan third party library which can help you to find the jar file. If you are not interested to use third party then you can write your own shell script to do the same job. The following shell script helps you to find the jar file which has the java class file.
#!/bin/bash #This script is to search jar file for java class file. USAGE="Usage: `basename $0` [-hw] <class-name> <dir-path> -h For help -w exact class name match e.g : ./`basename $0` -w Activity ./android-sdk-linux " #parse command line options while getopts hw OPT;do case "$OPT" in h) echo -e "$USAGE" exit 0 ;; w) specific=1 ;; \?) #getopts issues an error echo -e "$USAGE" >&2 exit 1 ;; esac done shift `expr $OPTIND - 1` #echo "print 1 : $1" if [ "$specific" -eq 1 ]; then pattern="/$1\\.class" else pattern=$1 fi shift #echo "pattern : $pattern" for I in $(find $* -type f -name "*.jar") do match=`jar -tf $I | grep $pattern` if [ ! -z "$match" ] then echo "Found in: $I" echo "$match" fi done
Example command :
./jarscan.sh -w Activity ~/Installed-software/android-sdk-linux/
You can also get the source code from Github.
Hope this blog helped you in some way. If you like this blog then please share it. You can also leave your comment below. You can find Facebook page here.