Find a class somewhere inside dozens of JAR files?

Unix

On Linux, other Unix variants, Git Bash on Windows, or Cygwin, use the jar (or unzip -v), grep, and find commands.

The following lists all class files that match a given name:

for i in *.jar; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done

If you know the entire list of Java archives you want to search, you could place them all in the same directory using (symbolic) links.

Or use find (case sensitively) to find the JAR file that contains a given class name:

find path/to/libs -name '*.jar' -exec grep -Hls ClassName {} \;

For example, to find the name of the archive containing IdentityHashingStrategy:

$ find . -name '*.jar' -exec grep -Hsli IdentityHashingStrategy {} \;
./trove-3.0.3.jar

If the JAR could be anywhere in the system and the locate command is available:

for i in $(locate "*.jar");
  do echo "$i"; jar -tvf "$i" | grep -Hsi ClassName;
done

A syntax variation:

find path/to/libs -name '*.jar' -print | \
  while read i; do jar -tvf "$i" | grep -Hsi ClassName && echo "$i"; done 

Windows

Open a command prompt, change to the directory (or ancestor directory) containing the JAR files, then:

for /R %G in (*.jar) do @jar -tvf "%G" | find "ClassName" > NUL && echo %G

Here's how it works:

  1. for /R %G in (*.jar) do - loop over all JAR files, recursively traversing directories; store the file name in %G.
  2. @jar -tvf "%G" | - run the Java Archive command to list all file names within the given archive, and write the results to standard output; the @ symbol suppresses printing the command's invocation.
  3. find "ClassName" > NUL - search standard input, piped from the output of the jar command, for the given class name; this will set ERRORLEVEL to 1 iff there's a match (otherwise 0).
  4. && echo %G - iff ERRORLEVEL is non-zero, write the Java archive file name to standard output (the console).

Web

Use a search engine that scans JAR files.


Eclipse can do it, just create a (temporary) project and put your libraries on the projects classpath. Then you can easily find the classes.

Another tool, that comes to my mind, is Java Decompiler. It can open a lot of jars at once and helps to find classes as well.