64

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 "com.ibm.websphere.security.auth.WSSubject", how do you track down the appropriate jar file? ("google" is not the answer I'm looking for!)

The 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.

aperkins
  • 12,914
  • 4
  • 29
  • 34
Jeffrey Knight
  • 5,888
  • 7
  • 39
  • 49
  • Are you attempting to resolve a dependency? i.e. when you run your code, it cannot find a class, so you are looking to put it on the classpath? – aperkins Sep 30 '09 at 19:23
  • Yes, that is exactly what I am trying to do. – Jeffrey Knight Sep 30 '09 at 19:24
  • I think you're asking the wrong question. I think the question you mean to ask is something like "I'm trying to develop a thin client application using WAS v6.1. Which jar should I include on my JVM class path to perform authentication (presumably WSSubject)?" Depending on what you're actually trying to do, searching the InfoCenter for "thin client" or "thin client wssubject" is probably what you're looking for. If you provide details, you can probably get the correct jar rather than just guessing based on the answers below. – Brett Kail Oct 01 '09 at 04:22
  • This should be tagged Linux/Unix - no use in Windows. – orbfish Jan 14 '14 at 16:26
  • Possible duplicate of [Find a class somewhere inside dozens of JAR files?](http://stackoverflow.com/questions/1342894/find-a-class-somewhere-inside-dozens-of-jar-files) – rogerdpack Oct 08 '15 at 23:25

18 Answers18

84

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.

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
  • I'm not sure if you or Dave Costa answered first, but this does the trick and I can use this script going forward. Thanks! – Jeffrey Knight Oct 01 '09 at 05:11
  • Dave answered first. His answer is simpler and will run quicker, but I think mine is slightly more flexible as you don't have to specify the fully-qualified class name. – Dan Dyer Oct 01 '09 at 10:34
  • 1
    This should be tagged Linux/Unix - no use in Windows. – orbfish Jan 14 '14 at 16:26
  • This is really great but produces error messages if some of the jar files happen to be broken symlinks. I built up on it and provided a version that address this problem as a separate answer (too long to add here as a comment): https://stackoverflow.com/a/47437943/274677. – Marcus Junius Brutus Nov 22 '17 at 15:12
14

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.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72
14

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.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
5

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
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
5

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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • - jarhoo costs money - docjar doesn't find "WSSubject" from my example (http://www.docjar.com/docs2web/s.jsp?q=WSSubject&t=q&start=0). There must be a 'search local jars' approach typically used by java devs .. ? – Jeffrey Knight Sep 30 '09 at 19:31
  • I'm hoping for an offline option. I often find myself dealing with somewhat obscure jar's from 3rd party vendors. – Jeffrey Knight Sep 30 '09 at 19:33
  • AFAIK, there is nothing built-in under Windows that can help for this. So, if you exclude cygwin, Googke Desktop may be the best (the less worst?) option. – Pascal Thivent Sep 30 '09 at 19:53
  • @PascalThivent. Check www.Help4j.com it will provide jar as well as source. You can include this link as well in this answer. – Ashish Aggarwal Feb 20 '15 at 06:09
  • Sadly, all gone in 2022. Javacio.us still exists, but has Indonesian ("Java") text touting internet gambling. Merobase.com still seems related to code, but I see no accessible search. I guess I'll have to wait for the MetaVerse versions to appear – LarryW Nov 08 '22 at 21:49
5

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 {}"
S M
  • 51
  • 1
  • 2
3

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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
3

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)

Joseph
  • 2,155
  • 6
  • 20
  • 32
  • There is also http://www.jarfinder.com. But they both seem to offer rather obsolete versions of jars. In conrast http://grepcode.com/ stays current on public code but can be used only as a hint on where to look and what the actual jar name is. – Vadzim Dec 19 '11 at 17:20
  • All three are gone as of November 2022 ... but you can buy JarFinder.com for the low, low price of 8,995 USD. I guess I should start clicking on the ads at mvnrepository.com – LarryW Nov 08 '22 at 21:54
2

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
Steve B.
  • 55,454
  • 12
  • 93
  • 132
1

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.

Mike Pone
  • 18,705
  • 13
  • 53
  • 68
  • Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community. If you're doing this then it usually means the questions are duplicates so flag them as such instead. – Kev May 10 '12 at 01:14
  • for usage with windows, I've put jarscan.jar into PATH together with jarscan.bat that contains `java -jar %~dp0jarscan.jar -class %1 %2 %3 %4 %5 %6 %7 %8 %9` - then it can be used just with "jarscan ServletRequest" – eis Sep 13 '12 at 12:58
1

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.

aperkins
  • 12,914
  • 4
  • 29
  • 34
1

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"}
WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
1

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

Ron Tuffin
  • 53,859
  • 24
  • 66
  • 78
0

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • Good answer for *nix, but it doesn't help on windows (though judging by his example *nix would be fine). – C. Ross Sep 30 '09 at 19:28
  • Fair enough, but what's the standard approach for a java developer on a windows machine that doesn't involve installing cygwin? – Jeffrey Knight Sep 30 '09 at 19:30
  • Find in files, *.jar, with the class as the string? (don't actually know if this works, but if grep works, this should) – TREE Sep 30 '09 at 19:45
0

I recommend using Maven, Eclipse and m2eclipse.

Step 1 - add specific import

alt text

Step 2 - find and download (automatically) desired jar

alt text

Spooky
  • 2,966
  • 8
  • 27
  • 41
cetnar
  • 9,357
  • 2
  • 38
  • 45
0

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

Som
  • 1,467
  • 13
  • 11
0

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
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
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

Amir
  • 1,638
  • 19
  • 26