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.