The Source for Java Technology Collaboration
User: Password:



Tom Ball

Tom Ball's Blog

JFind: a simple jar file locator

Posted by tball on September 03, 2005 at 01:24 PM | Comments (13)

I'm a big fan of the Unix find utility for managing large projects. One task that's difficult to do with it, however, is finding which jar file contains a Java class -- you can't use search commands like grep because it will match on any class that references your target, not just the defining class itself. There are several utilities that do this on the web, but most are big GUI-based tools which seem like overkill for what should be a small job.

So a while back I wrote a simple command-line utility called JFind, which is now in the NetBeans repository here. It's a single, simple class, which relies on the java.io and java.util.zip packages to do the heavy lifting. Just specify the class or package you want to find and a directory to start your search, and it will report the jar file or path that defines it.

For example, to find which jar provides org.openide.ErrorManager in a NetBeans distribution, run:

    java -jar jfind.jar org.openide.ErrorManager dist
To find which jar provides the javax.crypto package in the JDK:
    java -jar jfind.jar -p javax.crypto JDK-directory
One could write a more complex, Swiss-army-knife-like utility (like the Unix find utility), but sometimes short and sweet works best.

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • One could write a more complex, Swiss-army-knife-like utility (like the Unix find utility), but sometimes short and sweet works best.

    Or one could write a simpler 10 line shell-script for the same task :-):

    #!/bin/bash
    #
    # zip_grep - grep file name in zip files
    #
    # By Felipe Leme  - 11/19/2001
    # $Id: zip_grep,v 1.1 2001/11/21 16:33:41 felipeal Exp $
    #
    
    PROGNAME=`echo $0 | sed 's#^.*/##'`
    if [ $# -lt 2 ]
    then
        echo "Usage: ${PROGNAME}  "
        exit -1
    else
        PATTERN=$1
        shift
        FILES="$@"
    fi
    for FILE in ${FILES}
    do
       echo "Trying ${FILE}"
       unzip -t ${FILE} | grep ${PATTERN}
    done
    

    This is a very useful script, even though it requires you to pass all jars that it should scan (different from JFind, where you can just pass a directory) - but it would be easy to modify it to accept a dir - I just never felt the need for that (and I've been using it for 4 years, as shown in the header), as typically the jars are in the same dir and then a *.jar is enough.

    -- Felipe

    Posted by: felipeal on September 03, 2005 at 02:07 PM

  • Also see www.jarhoo.com; it's probably trivial to write a Firefox search engine to search it directly.

    Posted by: ljnelson on September 03, 2005 at 02:47 PM

  • felipe,
    Very cross-platform indeed. You may not be aware, but Windows still holds 70-80% of the market. According to the access logs to my java.net projects, 82% is Windows (various flavors), 12% is Unix (Linux + SunOS), 2% is Mac (and the rest is unidentified). Now, you may argue that everybody uses Windows box for surfing the net, and Unix monster to develop apps, and you would be right of course... Who doesn't?

    Posted by: kirillcool on September 03, 2005 at 11:59 PM

  • Let me reply inverting the order...

    You may not be aware, but Windows still holds 70-80% of the market.
    I am - and unfortunately have to use it at work (ironically enough, at home I have Linux on the desktop and MacOS on the laptop...)

    Very cross-platform indeed.
    Well, bash is kind-of cross-platform - for instance, I use cygwin at work. It is akward in some situations (for instance, symbolic links work inside cygwin but not outside it), but it is sufficient for the most cases...

    Anyway, I wasn't criticizing JFind, just commenting the good-old shell-script is an invalueble tool...

    Posted by: felipeal on September 05, 2005 at 06:37 AM

  • GCJ includes grepjar which does something similar. I think it is included in the MinGW version of GCJ for Windows.

    Posted by: tlaurenzo on September 05, 2005 at 07:48 AM

  • Since the original question here was not portability, but rather the ability to achieve a certain goal, I want to say that this particular task is easily performed by "bash" shell, given that "jar" is in the path:

    for i in `ls *.jar`; do (jar tf $i | grep -q ) && echo $i;done

    Of course, "ls *.jar can be replace by your favourite "find"...

    Posted by: lpintilie on September 06, 2005 at 11:42 AM

  • This discussion is hilarious, as it reminds me of when my team once spent a week arguing over the nuances of the perfect find/xargs/grep combination. My only point was to offer a simple solution that reduces the number of keystrokes/time to get some information. I'm surprised a Perl expert hasn't weighed in yet! How about if we all get back to work now, or we'll waste more time arguing than the sum total of our of our timesavers? :-)

    Posted by: tball on September 06, 2005 at 12:22 PM

  • I respectfully disagree. Some of the people who posted comments thought you stated a false thing (it is not easy to use UNIX tools for a certain task) and they wanted to prove the contrary. Why is this hilarious? Some tasks are completed faster when you can make use of existing tools (in this case, UNIX utilities), rather than writing a new Java utility for that particular task. I am not criticizing your preference for tha later approach. I just think your "hilarious" word is inappropriate.

    Posted by: lpintilie on September 07, 2005 at 06:50 AM

  • Perhaps this is an English language problem, as "hilarious" is rarely meant in an offensive manner. I found the situation funny, but was not dismissing the participants' valid opinions. I am sorry if I inadvertently offended anyone -- perhaps it's time for me to take Sun's "Communicating Across Cultures" course again!

    Posted by: tball on September 07, 2005 at 07:03 AM

  • Hi Tim. I didn't mean to sound THAT offended. I don't think "hilarious" is in line with your original posting, that's all. And you are right, it might be a problem of English language, but I also believe it's a problem of communicating in writing: the written word loses important attributes, like the tone of the speaker. And now I realized that time-saving utilities allow us to spend more time on communication issues. :) Who says communication failures aren't a serious threat to keeping a project on track? :) (After all, the original post started with "managing large projects".)

    Posted by: lpintilie on September 07, 2005 at 07:33 AM

  • A few years ago I ran into the same problems developing applications for J2EE where we not only have a ton of archives but also archives like EARS that contains archives within archives. Therefore I created a simple tool to search a class or a package in one or more archives and then also search recursively within sub archives (like JAR,WAR and RAR in an EAR).
    But that was not quite enough for J2EE environment and I created another tool that does extract an archives recursively so that it can be altered and later the archive can be recreated without losing any sweat. This is helpful when you need to adjust properties files or descriptors in an EAR file. Unfortunately I never found time to make it nice enough for others to use and also I never found a platform where such a simple tool can be released without forcing the user to download a huge application with it.
    -Andy

    Posted by: schaefa on September 07, 2005 at 01:44 PM

  • Where can I get the source code for jfind.jar. Also, can anyone please provide a similar utility for Windows (given a class name, I would like to know what all jar files contain that class) Thanks Sri

    Posted by: vatsa82 on May 07, 2007 at 05:24 PM

  • From the NetBeans source repository, checkout "contrib/jfind". It runs on all platforms that run Java, including Windows.

    Posted by: tball on May 08, 2007 at 06:55 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds