#!/bin/csh -f # findclassinjars # ------------------------------------------------------------------------- # Shell script to find a specified class in a set of jar files, or on # the CLASSPATH, or in a directory tree containing jar files. # ------------------------------------------------------------------------- # Revision History: # Anticipated Changes: # - Also search other jars in same folder as specified jars? # - Also search jars in jars? Useful? # - Also search jars in zips, and nested zips. # $Log$ # ------------------------------------------------------------------------- if ($#argv < 2 || $1:q == "-h" || $1:q == "--help") then echo "Usage: $0:t [options] classname jarname..." echo " where:" echo " classname = Name or partial name of class (or any file)" echo " jarname... = Zero or more jar (or tar) filenames" echo " options:" echo " -i = Ignore case in class/file name" echo " -c = Also search all jar/tars on the CLASSPATH" echo " (Same as -e CLASSPATH)" echo " -e env_var" echo " = Also search all jar/tars in the colon-separated" echo " value of the specified environment variable" echo " (good for searching CLASSPATH, PATH, etc.)" echo " -p colon_separated_list_of_jars" echo " = Also search all jar/tars in the specified" echo " colon-separated list of jar/tars" echo " -r = Also search current directory recursively" echo " -v = Verbose. Show name of each jar/tar searched" echo " -h = Show this help info" echo " --help = Show this help info" echo "Example: $0:t ServiceException *.jar" echo "Example: $0:t javax/xml/rpc/ServiceException *.jar" echo "Example: $0:t javax/xml/rpc/ServiceException.class *.jar" echo "Example: $0:t some_file_name *.tar" echo "Example: $0:t iceExcep *.jar" echo "Example: $0:t -i iceEXCEP *.jar" echo "Example: $0:t -c javax/xml/rpc/ServiceException" echo "Example: $0:t -e OTHER_CLASSPATH javax/xml/rpc/ServiceException" echo "Example: $0:t -p '1.jar:2.jar:jars/3.jar' javax/xml/rpc/ServiceException" echo "Example: $0:t -r javax/xml/rpc/ServiceException" echo "Example: $0:t -i -r -v serviceexception *.jar" exit 1 endif # Collect command line options set classpath_option = "" set env_var_option = "" set env_var_name = "" set path_option = "" set path_value = "" set ignore_case_option = "" set recursive_option = "" set verbose_option = "" while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then shift # If help option was anywhere among the options, call recursively # with just that option, and exit. $0:t --help exit 1 else if ("$1" == "-i") then shift set ignore_case_option = "-i" else if ("$1" == "-c") then shift set classpath_option = "-c" else if ("$1" == "-e") then shift set env_var_option = "-e" if ("$1" == "") then beep "Option $env_var_option requires a value" $0:t --help exit 1 else set firstchar = "`echo $1:q | cut -c 1`" if ("$firstchar" == "-") then beep "Option $env_var_option requires a value" $0:t --help exit 1 else set env_var_name = "$1" shift endif endif else if ("$1" == "-p") then shift set path_option = "-p" if ("$1" == "") then beep "Option $path_option requires a value" $0:t --help exit 1 else set firstchar = "`echo $1:q | cut -c 1`" if ("$firstchar" == "-") then beep "Option $path_option requires a value" $0:t --help exit 1 else set path_value = "$1" shift endif endif else if ("$1" == "-r") then shift set recursive_option = "-r" else if ("$1" == "-v") then shift # Note: Use the option value itself as the value of verbose_option, # not "true" or something so that we can pass it to this # script for verbose recursive calls. set verbose_option = "-v" else # Not a recognized option. Assume it's the first parameter break endif end if ($#argv == 0) then beep "No classname specified" $0:t --help exit 1 endif set classname = $1:q shift foreach jarfile ($*:q) if ("$verbose_option" != "") then echo "Searching ${jarfile}..." endif if ("${jarfile:e}" != "jar" && "${jarfile:e}" != "tar") then beep "Warning: $jarfile doesn't end in '.jar' or '.tar'." endif if (-r ${jarfile:q}) then set found_name = "`jar -tvf ${jarfile:q} | grep ${ignore_case_option} ${classname}`" if ("${found_name}" != "") then echo "${jarfile}: ${found_name}" endif else beep "${jarfile} is not readable." endif end if ("$classpath_option" != "") then $0 $ignore_case_option $verbose_option -e "CLASSPATH" $classname endif endif if ("$env_var_option" != "") then if ("$verbose_option" != "") then echo "Starting search of $env_var_name..." endif set env_var_value = "`printenv ${env_var_name}`" if ("$env_var_value" == "") then beep "Environment variable $env_var_name has no value" else $0 $ignore_case_option $verbose_option -p "$env_var_value" $classname endif endif if ("$path_option" != "") then if ("$verbose_option" != "") then echo "Starting search of '$path_value'..." endif if ("$path_value" != "") then $0 $ignore_case_option $verbose_option $classname `echo $path_value | split_at_chars :` endif endif if ("$recursive_option" != "") then if ("$verbose_option" != "") then echo "Starting recursive search..." endif find . -name \*.jar -exec $0 $ignore_case_option $verbose_option $classname "{}" ";" endif