how to find the jar file containing a class definition?

40,946

Solution 1

(This is an improvement over the script I had in previous versions of the answer as it produces much cleaner output at the price of some awk special/ugly quoting.)

I've built a script (findinjars) which does just that.

#!/usr/bin/env bash
if [[ ($# -ne 1) && ($# -ne 2) ]]
then
    echo "usage is $0 <grep pattern to look for in 'jar tvf' output> [<top-of-dir-tree> or, if missing, current dir]"
else
    THING_TO_LOOKFOR="$1"
    DIR=${2:-.}
    if [ ! -d $DIR ]; then
        echo "directory [$DIR] does not exist";
        exit 1;
    fi
    find "$DIR" -iname \*.jar | while read f ; do (jar tf $f | awk '{print "'"$f"'" "  " $0}' | grep -i "$THING_TO_LOOKFOR") ; done
fi

you can then invoke it with:

$findinjars a.b.c.d.Class [directoryTreeRoot or, if missing, current dir]

or just

$findinjars partOfClassName [directoryTreeRoot or, if missing, current dir]

Dot characters in the fully qualified class name will be interpreted in the regexp sense of 'any character' but that's not a big deal since this is a heuristics utility (that's why it's case-insensitive too BTW). Usually I don't bother with the full class name and just type part of it.

Solution 2

In the same lines as BalusC's answer (I can't post comment yet nor link 2 urls, no reputation :( ), you can find a jar thanks to these 2 jar finder engines: - http://www.jarfinder.com/ - findjar

Solution 3

A simpler command. You don't have to use 'while-do', use '&&' to print file name if 'grep' finds the class.

find . -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep Hello.class &&  echo {}'

Solution 4

I usually employ bash for that: grep -lr "ClassName" . The trick is that names aren't encoded in any way. You can open jar file in text editor and you'll see them. (You can even include package name in search query.)

I suspect, there's some windows equivalent too.

Solution 5

I ran into the same problem countless times, so I wrote a little tool to find them.

A simple command line: findclass MyClass -classpath ...

It searches directories, jar, zip, war and ear files for your class. I wrote it a few years back and use it all the time. I thought others might find it useful too so I uploaded onto github.

It's freely available to download: https://github.com/eurozulu/Findclass/tree/master/dist

If you want to look at the source, it's in the same site: https://github.com/eurozulu/Findclass/tree/master

If you like it, just let me know to give me that warm comfy feeling :)

Share:
40,946
luiscolorado
Author by

luiscolorado

Currently looking for Alfresco projects and other opportunities in Java/JEE development and architecting. Alfresco Solr and Performance are my specialties. Bring on the questions!

Updated on February 20, 2020

Comments

  • luiscolorado
    luiscolorado about 4 years

    Is there a tool, plugin, or script I can use to find a java class in a bunch of jar files?

    Very often I inherit code that complains about a class that doesn't exist, and it is just because the jar file is not included in the classpath. But, in what jar file(s) is the class? I may not have the JAR (so I have to search online), or adding a JAR to the classpath could create a duplicated class definition problem.

    I obviously would prefer an eclipse plugin, but I'm open to any piece of software that works with Windows.

    I know... Windows is not my choice, but that's what I got to work with.

    Thanks!

    Luis

    P.S. Thank you for your answers. After reviewing some responses, I became aware that I should have explained better my scenario. We had a library of downloaded or created JAR files, but sometimes the class would be online somewhere.