views:

2057

answers:

11

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.

+1  A: 

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
Jeffrey Knight
I'm hoping for an offline option. I often find myself dealing with somewhat obscure jar's from 3rd party vendors.
Jeffrey Knight
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
A: 

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

Paul Tomblin
Good answer for *nix, but it doesn't help on windows (though judging by his example *nix would be fine).
C. Ross
Fair enough, but what's the standard approach for a java developer on a windows machine that doesn't involve installing cygwin?
Jeffrey Knight
Find in files, *.jar, with the class as the string? (don't actually know if this works, but if grep works, this should)
TREE
+2  A: 

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
Note that jar files are zip files, and the filename in the jar effectively map to the package and class.
TREE
+5  A: 

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
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
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
+1  A: 

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.
A: 

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
A: 

Jar Finder

Although it did not work for com.ibm.websphere.security.auth.WSSubject, I have had it work for a lot of other classes.

Zombies
A: 
cetnar
A: 

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
+1  A: 

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 {}"
Stephen Muench
A: 

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)

Dan