views:

63

answers:

6

What is your favorite tool, plugin, script, 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 is the class?

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!

A: 

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.

Nikita Rybak
Yeah, but the problem is that it's hard to tell if the file contains a definition or a use of the class.And, yes, there's a grep utility that can be downloaded.Thanks!
luiscolorado
@luiscolorado It's never given me false positive before. I don't know, probably most of content (including referenced classes) is encoded in some way.
Nikita Rybak
Mmmmmhh... Unfortunately I got a bunch of false positives. Or maybe it's because the class has the same name across multiple packages.In any case, I'll give it another shot. Thanks, Nikita!
luiscolorado
A: 

You could always add the Reference library to your project in Eclipse and then in Package Browser, just expand the packages in the JAR file until you find the class that you are looking for.

arunabhdas
Certainly! Although I was looking for something faster :) I must have at list 20 jar files in this project, and who knows how many classes.
luiscolorado
For jars on the build path, just use Ctrl-Shift-T to locate a class.
Thorbjørn Ravn Andersen
Still not my cup of tea, but we'll see.
luiscolorado
A: 

@Nikita Rybak: AstroGrep for Windows is our friend: http://astrogrep.sourceforge.net/

NinjaCat
I liked AstroGrep! I found it friendly compared with the command line version. However, it has the same problem that any grep (see my comment to Nikita Rybak).
luiscolorado
True... _but_ with AstroGrep you can set it to return +/- N number of lines. So it's certinaly not the best option, but it might work.
NinjaCat
A: 

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.

If it's not in the classpath, then you likely don't have the JAR file itself at all. Searching via Eclipse's builtin Ctrl+Shift+T function won't help much. Usually you can make use of the package name to "guess" where you could get the JAR file from at the internet. E.g. a org.apache.commons.lang.XXX class is available at http://commons.apache.org/lang.

For the unobvious ones, I myself use http://www.jarhoo.com, the JAR search engine (which is unfortunately unavailable at that moment!).

BalusC
_If it's not in the classpath, then you likely don't have the JAR file itself at all_ Well, it often happened to me with jboss. Hundreds of internal libs and you often need few of them to compile your code. Thanks for the link, though, that's interesting.
Nikita Rybak
@Nikita: In that specific case, you'd rather like to update the target server of your dynamic web project to JBoss.
BalusC
A: 

use this:

public class Main {

    private static final String SEARCH_PATH = "C:\\workspace\\RPLaunch";
    private static String CLASS_FILE_TO_FIND =
            "javax.ejb.SessionBean";
    private static List<String> foundIn = new LinkedList<String>();

    /**
     * @param args the first argument is the path of the file to search in. The second may be the
     *        class file to find.
     */
    public static void main(String[] args) {
        File start;
        new Scanner(args[0]);
        if (args.length > 0) {
            start = new File(args[0]);
            if (args.length > 1) {
                CLASS_FILE_TO_FIND = args[1];
            }
        } else {
            start = new File(SEARCH_PATH);
        }
        if (!CLASS_FILE_TO_FIND.endsWith(".class")) {
            CLASS_FILE_TO_FIND = CLASS_FILE_TO_FIND.replace('.', '/') + ".class";
        }
        search(start);
        System.out.println("------RESULTS------");
        for (String s : foundIn) {
            System.out.println(s);
        }
    }

    private static void search(File start) {
        try {
            final FileFilter filter = new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(".jar") || pathname.isDirectory();
                }
            };
            for (File f : start.listFiles(filter)) {
                if (f.isDirectory()) {
                    search(f);
                } else {
                    searchJar(f);
                }
            }
        } catch (Exception e) {
            System.err.println("Error at: " + start.getPath() + " " + e.getMessage());
        }
    }

    private static void searchJar(File f) {
        try {
            System.out.println("Searching: " + f.getPath());
            JarFile jar = new JarFile(f);
            ZipEntry e = jar.getEntry(CLASS_FILE_TO_FIND);
            if (e == null) {
                e = jar.getJarEntry(CLASS_FILE_TO_FIND);
            }
            if (e != null) {
                foundIn.add(f.getPath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
ILMTitan
+5  A: 

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

Fanny H.
jarfinder *IS* your friend.
Tom