[ACCEPTED]-Find a jar file given the class name?-dependencies

Accepted answer
Score: 84

Save this as findclass.sh (or whatever), put 10 it on your path and make it executable:

#!/bin/sh
find "$1" -name "*.jar" -exec sh -c 'jar -tf {}|grep -H --label {} '$2'' \;

The 9 first parameter is the directory to search 8 recursively and the second parameter is 7 a regular expression (typically just a simple 6 class name) to search for.

$ findclass.sh . WSSubject

The script relies 5 on the -t option to the jar command (which 4 lists the contents) and greps each table 3 of contents, labelling any matches with 2 the path of the JAR file in which it was 1 found.

Score: 14

There is no "official" Java way to do this 6 AFAIK.

The way I usually hunt for it is to 5 use find and jar to look through all jar files 4 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 3 don't want to install Cygwin, then I suppose 2 you would have to write a batch script to 1 locate the jar files.

Score: 14

I have written a program for this: https://github.com/javalite/jar-explorer It will 3 also decompile existing byte code to show 2 you interfaces, methods, super classes, will 1 show contents of other resources - text, images, html, etc.

Score: 5

If the grep on your system (e.g. Solaris) doesn't 2 have -H and --label as used in Dan Dyer's 1 example, you can use:

find . -name '*.jar' -type f | xargs -i bash -c "jar -tvf {}| tr / . | grep WSSubject && echo {}"
Score: 5

To search all jars under the current directory 5 and return the one(s) that contain class 4 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 3 of class a.b.c.D (or classes with a similar 2 suffix) and will only print the jars that 1 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
Score: 4

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.

0

Score: 3

In Windows, run cmd.exe and type:

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

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

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

Score: 3

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

Score: 2

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

e.g.

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

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

Score: 1

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

Score: 1

In windows powershell you can use this command. It 3 list all the JAR files it encounters, but 2 it has an extra line that's very easy to 1 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"}
Score: 1

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

Score: 0

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

0

Score: 0

I recommend using Maven, Eclipse and m2eclipse.

Step 2 1 - add specific import

alt text

Step 2 - find and 1 download (automatically) desired jar

alt text

Score: 0

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

Score: 0

Building up on Dan's excellent answer, the following script solves 5 the problem of mangled output in case some 4 of the jars are actually broken symlinks 3 (while at the same time not skipping proper 2 symlinks) It also searches in the current 1 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
Score: 0

in Intellij Idea

  1. on your class press ctrl+B and after that you can find the jar file.
  2. on project bar press scroll from source.
  3. you can see the jar file contains the class.

enter image description here

0

More Related questions