Posted By

cruelfate on 01/07/11


Tagged

Bash java search classpath


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cruelfate


class.sh


 / Published in: Bash
 

Find class or resource in jar files and/or directories of jars/zips.

  1. #! /bin/bash
  2.  
  3. function usage {
  4. echo 'Find class or resource in jar files and/or directories of jars.'
  5. echo ''
  6. echo 'usage: $0 pattern files... dirs...'
  7. echo ''
  8. exit
  9. }
  10.  
  11. function fqcn2path {
  12. fqcn=$1
  13. path=`echo $fqcn | sed -e 's@\.@/@g'`
  14. echo $path
  15. }
  16.  
  17.  
  18. target=/usr/java/jdk/jre/lib/rt.jar
  19. pattern=
  20. quiet=
  21. while getopts e:q c
  22. do
  23. case $c in
  24. 'q') quiet=t ; break ;;
  25. '?') # any other switch
  26. usage
  27. ;;
  28. esac
  29. done
  30.  
  31. shift `expr $OPTIND - 1`
  32.  
  33. if [ $# -eq 0 ] ; then
  34. usage
  35. fi
  36.  
  37. pattern=`fqcn2path "$1"`
  38. shift 1
  39.  
  40. if [ $# -gt 0 ] ; then
  41. target=$*
  42. fi
  43. declare cygwin=""
  44. case "`uname -o`" in
  45. Cygwin*) cygwin=true ;;
  46. *) ;;
  47. esac
  48.  
  49.  
  50. function look {
  51. if [ "$quiet" = "" ] ; then
  52. echo $1 . .
  53. fi
  54. if [ -n "$cygwin" ] ; then
  55. p="`cygpath -wa $1`"
  56. else
  57. p="$1"
  58. fi
  59. if [ -r $1 ] ; then
  60. if [ "$pattern" == "" ] ; then
  61. jar tvf "$p"
  62. else
  63. jar tvf "$p" | fgrep $pattern
  64. fi
  65. else
  66. echo ERROR: Cannot read $1
  67. fi
  68. }
  69.  
  70. for t in $target
  71. do
  72. if [ -d $t ] ; then
  73. for f in `find $t -type f -name "*.jar" -o -name "*.zip"`
  74. do
  75. look $f
  76. done
  77. else
  78. look $t
  79. fi
  80. done

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: cruelfate on January 7, 2011

Also works with Cygwin on Windows

Posted By: cruelfate on February 1, 2011

Find class or resource in jar files and/or directories of jars/zips.

Usage: class.sh pattern files..

You need to login to post a comment.