Quick one

May 26th, 2008 by rafaelliu Leave a reply »

This one is for all of you who (like me) keeps wasting time using commands such as find, jar and grep each time you get a classpath error:

#!/bin/bash
if [ "$1" = "" ]; then
	echo "Usage: jarfind REGEXP";
	exit;
fi
 
for d in `find . -name '*.jar'` ; do
	FILES=`unzip -l $d | cut -c 29- | egrep ''$1''`;
 
	if [ "$FILES" != "" ]; then
		echo "$d";
 
		for f in $FILES ; do
			echo " - $f"
		done
	fi
done

I’ve chose to use unzip instead of jar since it’s far more commonly seem. Move it to /usr/local/bin/jarfind (well, that’s a little personal) and chmod it to +x.

I works searching all jars under the current directory for a regular expression, most commonly a simple class name. It returns the jar’s names and all corresponding class matches.

I’m not a bash programmer, so it may not look that pretty to experienced bash programmers, but it looks beautiful to me, as it saves me a lot of time =). Feel free to make any comments.

EDIT:

first known bug: doesn’t escape special characters in paths =/

Advertisement

1 comment

  1. Hi Rafael,

    Great solution… I usually do the following command (very simple too) to get similar results:


    $ find . -type f -name \*.jar -exec bash -c "echo {}; jar tvf {} | grep SomeClass" \;

    []s!
    PJ

Leave a Reply