Find a jar file given the class name?

Solution 1:

Save this as findclass.sh (or whatever), put it on your path and make it executable:

#!/bin/sh
find "$1" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} '$2'' \;

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.

$ findclass.sh . WSSubject

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.

Solution 2:

There is no "official" Java way to do this AFAIK.

The way I usually hunt for it is to use find and jar to look through all jar files in a given tree.

> find . -name \*.jar -print -exec jar tf {} oracle/sql/BLOB.class \;
./v2.6.1/lib/csw_library.jar
./v2.6.1/lib/oracle_drivers_12_01.jar
oracle/sql/BLOB.class

If you're on Windows and don't want to install Cygwin, then I suppose you would have to write a batch script to locate the jar files.

Solution 3:

I have written a program for this: https://github.com/javalite/jar-explorer It will also decompile existing byte code to show you interfaces, methods, super classes, will show contents of other resources - text, images, html, etc.

Solution 4:

If the grep on your system (e.g. Solaris) doesn't have -H and --label as used in Dan Dyer's example, you can use:

find . -name '*.jar' -type f | xargs -i bash -c "jar -tvf {}| tr / . | grep WSSubject && echo {}"