Drowning in a sea of JARs
You know the situation, you’ve got a NoClassDefFoundError but you’re not sure which of 20+ JAR files you need to add to fix it. This is a question that has cropped up a few times on the Java newsgroups. There are a few solutions to this problem. A colleague of mine has a 60-line Java utility that he’s pleased with, but then terseness is not Java’s strong point. Here’s the equivalent shell script:
#!/bin/sh find "$1" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} '$2'' \;
(NOTE: The command may be wrapped when it is displayed on this page, but everything from “find…” should be on a single line).
Save this as findclass.sh (or whatever), put it on your path and make it executable. Problem solved.
The first parameter is the directory to search recursively and the second parameter is a regular expression (typically just a simple class name) to search for. The script relies on the -t option to the jar command (which lists the contents) and greps each table of contents, labelling any matches with the path of the JAR file in which it was found.
For shell-impaired Windows users, Cygwin (possibly with Poderosa) is the answer.

on May 15th, 2007 at 1:05 am
Sea of jars indeed. http://www.youtube.com/watch?v=PQbuyKUaKFo
on May 15th, 2007 at 5:03 am
Gud job!!! It ll be really helpful…
on May 15th, 2007 at 5:57 am
i ve tried it…but its givin some error can u pls elaboarate it…
#!/bin/sh
find $1 -name “*.jar” -exec sh -c ‘jar -tf {“./jars”}|grep -H –label {‘naming’} $2′ \;
find: path-list predicate-list
on May 15th, 2007 at 9:38 am
My apologies. I added the parameters $1 and $2 from memory and forgot to quote them. I’ve edited it now, it should work fine.
on May 15th, 2007 at 4:09 pm
http://briankuhn.com/?p=44