X

Best Practices from Oracle Development's A‑Team

Finding which JAR contains a class – again!

Mark Nelson
Architect

I posted a few days ago about finding which JAR file contains a class file – something I often want to do.  My friend Chris Johnson promptly posted a better version here with caching – thanks Chris!

Chris said I inspired him to finish his script :-) and in return he has inspired me to improve it further.  As I often work with JAR files in a development environment, and often a lot of them, the search can take a while, so the caching is great.  But – those files can change over time, so I need the cache to be kept up to date.

Here is an updated version of Chris’ version that will update the cache if a JAR file is newer than the cache:

#!/bin/sh TARGET="$1" CACHEDIR=~/.jarfindcache # prevent people from hitting Ctrl-C and breaking our cache trap 'echo "Control-C disabled."' 2 if [ ! -d $CACHEDIR ]; then     mkdir  $CACHEDIR fi for JARFILE in `find $PWD -name "*jar"` do   CACHEFILE=$CACHEDIR$JARFILE   if [ ! -s $CACHEFILE ]; then       mkdir -p `dirname $CACHEFILE`       nohup jar tvf $JARFILE > $CACHEFILE   # if the jar file has been updated, cache it again   elif [ $JARFILE -nt $CACHEFILE ]; then       nohup jar tvf $JARFILE > $CACHEFILE   fi   grep $TARGET $CACHEFILE > /dev/null   if [ $? == 0 ]   then     echo "$TARGET is in $JARFILE"   fi done

P.S.  If you are running this on Ubuntu (or Debian), like me, you better make that first line a bit more explicit:

#!/bin/bash

Enjoy!

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha

Recent Content