Shell script to find a class in a set of Jar files


When you work on a Java project with many library dependencies it can be difficult to know which Jar files contain which classes. Hunting down a particular class in a tangled mess of Jar files can be painful, especially if you need to do so on a server over a shell connection.

Here is a handy shell script that iterates over all the Jar archives in a given path, finds those containing file names that match a given pattern, and prints out these matches followed by the name of the Jar file.


if [ -z "$2" ]
  echo Usage: $0 Directory ClassName
  exit 1

for f in $(find $1 -name '*.jar')
  jar tf $f | grep "$2" && echo "[in $f]"

Save this script to a file with an obvious name, like

To find all the classes with names containing "HttpClient" in the libs directory, you would invoke the script like so:

$ sh libs HttpClient

[in libs/commons-httpclient/commons-httpclient-3.1.jar]

If you are impatient, you can accomplish the same thing with a big and ugly one-liner:

for f in $(find libs -name '*.jar'); do jar tf $f | grep HttpClient && echo "[in $f]"; done
Tags: Coding