Find a jar file given the class name?

JavaJarDependencies

Java Problem Overview


This must be a very basic question for Java developers, but what is the best way to find the appropriate jar file given a class name?

For example, given "http://publib.boulder.ibm.com/infocenter/wasinfo/v5r1//index.jsp?topic=/com.ibm.websphere.wbifz.doc/info/wbifz/javadoc/ae/com/ibm/websphere/security/auth/WSSubject.html">com.ibm.websphere.security.auth.WSSubject</a>";, how do you track down the appropriate jar file? ("google" is not the answer I'm looking for!)

The http://publib.boulder.ibm.com/infocenter/wasinfo/v5r1//index.jsp?topic=/com.ibm.websphere.wbifz.doc/info/wbifz/javadoc/ae/com/ibm/websphere/security/auth/WSSubject.html">java docs do not give any hint of the jar file, and obviously the names of the jar files themselves offer no clue.

There must be a 'search local jars', or some sort of 'auto-resolve dependencies', trick in the java world. Ideally, I'm looking for the 'official' way to do this. I happen to be on a windows machine without cygwin.

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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 {}"

Solution 5 - Java

To search all jars under the current directory and return the one(s) that contain class a.b.c.D do a:

find . -iname *.jar | while read JARF; do jar tvf $JARF | grep a/b/c/D.class && echo $JARF ; done

It will report all instances of class a.b.c.D (or classes with a similar suffix) and will only print the jars that contain it.

Typical output:

$ find . -iname *.jar | while read JARF; do jar tvf $JARF | grep Log.class && echo $JARF ; done
479 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/Log.class
3714 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/Log4JCategoryLog.class
1963 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/NoOpLog.class
9065 Fri Oct 10 18:19:40 PDT 2003 org/apache/commons/logging/impl/SimpleLog.class
./WebContent/WEB-INF/lib/commons-logging.jar

Solution 6 - Java

You could try services like:

Or

Or

  • A maven enterprise repository with a search feature e.g. Nexus (OFC, this would only work if the jars you're looking for are indexed i.e. installed in the repository)

PS: Jarhoo has teamed up with Javacio.us to provide 100,000 Java developers with free access to Jarhoo via links integrated with their Google search results. Subscription to Javacio.us is free and open to anyone with a Google account. For more information, please visit the Jarhoo offer page at Javacio.us.

Solution 7 - Java

In Windows, run cmd.exe and type:

  for %i in (*.jar) do @jar tvf %i | find "/com/company/MyClass.class"

The jars would have to be in the current directory. For also has a /R option which takes a directory and lets you search recursively.

If Jar.exe isn't in your path, you can do something like @C:\jdk\bin\jar.exe.

Solution 8 - Java

Try findjar.com. If it's in the public domain, you should get it. There's alos mavenjava.com (but that site seems to be down)

Solution 9 - Java

Printing the list as I go so I can see what I'm checking. Mostly I'm looking in a lib/app directory, but you can substitute a locate for the find.

e.g.

for jar in $(find some_dir/lib -name "*.jar" ); 
do
echo -------------$jar-------------------
jar -tf $jar | grep TheNameOfTheClassImLookingFor
done

Solution 10 - Java

Given your comment on attempting to handle dependencies, what I would do is focus on which libraries you are using. Knowing this, you will know what jars have to be on the classpath, and you can pay attention to that. There are also dependency management builders (Maven and Ant being two popular ones) that can package up projects with their dependencies inside. However, in the end, it is up to the developer to know which dependencies they have, and to pay attention to the requirements for those dependencies. This is one reason why using an IDE like Eclipse, and build tools like Maven or Ant are so nice in large projects, as when you have 20 dependencies for a project, that can become pretty unmanageable.

Solution 11 - Java

I use jarscan. It is an executable jar file that can recursively search an entire folder structure for jars that contain the class that you are looking for. It searches by class name, package name or regex.

Solution 12 - Java

In windows powershell you can use this command. It list all the JAR files it encounters, but it has an extra line that's very easy to spot where it also finds your class.

Get-ChildItem -recurse -Filter *.jar | Foreach {echo $_.fullname; c:\somepath\JDK\BIN\jar tvf $_.fullname | Select-String -pattern "Cabbages.class"}

Solution 13 - Java

There is also this web site that seems to be usefull. http://www.findjar.com/

Solution 14 - Java

locate .jar | xargs grep com.ibm.websphere.security.auth.WSSubject

Solution 15 - Java

I recommend using Maven, Eclipse and m2eclipse.

Step 1 - add specific import

alt text

Step 2 - find and download (automatically) desired jar

alt text

Solution 16 - Java

if you are still searching for WSSubject, then jar is wssec.jar. WSSecurityException class inside sas.jar

Solution 17 - Java

Building up on Dan's excellent answer, the following script solves the problem of mangled output in case some of the jars are actually broken symlinks (while at the same time not skipping proper symlinks) It also searches in the current directory if no argument is provided.

#!/usr/bin/env bash

if [[ ($# -ne 1) && ($# -ne 2) ]]
then
    echo "usage is $0 <grep RegEx to look for in contents of jar> [<top-of-folder-hierarchy> or, if missing, current dir]"
else
    REG_EXP=$1
    DIR=${2:-.}
    if [ ! -d $DIR ]; then
        echo "directory [$DIR] does not exist";
        exit 1;
    fi
    find "$DIR" -name "*.jar" -exec sh -c '
    (test -e {})
    exitStatus=$?
    if [ "$exitStatus" -eq 0 ]; then # this is done to avoid broken symlinks
        jar -tf {}|grep -i -H --label {} '$REG_EXP'
    fi
' \;
fi

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeffrey KnightView Question on Stackoverflow
Solution 1 - JavaDan DyerView Answer on Stackoverflow
Solution 2 - JavaDave CostaView Answer on Stackoverflow
Solution 3 - JavaipolevoyView Answer on Stackoverflow
Solution 4 - JavaS MView Answer on Stackoverflow
Solution 5 - JavaMarcus Junius BrutusView Answer on Stackoverflow
Solution 6 - JavaPascal ThiventView Answer on Stackoverflow
Solution 7 - JavaYishaiView Answer on Stackoverflow
Solution 8 - JavaJosephView Answer on Stackoverflow
Solution 9 - JavaSteve B.View Answer on Stackoverflow
Solution 10 - JavaaperkinsView Answer on Stackoverflow
Solution 11 - JavaMike PoneView Answer on Stackoverflow
Solution 12 - JavaWoodenKittyView Answer on Stackoverflow
Solution 13 - JavaRon TuffinView Answer on Stackoverflow
Solution 14 - JavaPaul TomblinView Answer on Stackoverflow
Solution 15 - JavacetnarView Answer on Stackoverflow
Solution 16 - JavaSomView Answer on Stackoverflow
Solution 17 - JavaMarcus Junius BrutusView Answer on Stackoverflow