Java: How do I know which jar file to use given a class name?

Solution 1:

If you have the jar in your class path / project path hit CTRL-SHIFT-T and type the name ... the jar will be displayed at the bottom.

If you haven't the class in your build path a) put together a dummy project containing all the jars b) I think there is a plugin to find jars from IBM Alphaworks (but that might be kind of outdated)

Solution 2:

You also have this eclipse plugin: jarclassfinder

The user enters the name of the class not found (or the name of the class that the Java project needs to access). The plug-in will search the selected directory (and subdirectories) for JAR files containing that class.

All results are displayed in a table in a custom view. The user can then browse this table and select the JAR file to add to his Java project's build path. The user then right-clicks on the entry in the table and, from the context menu, selects the build path to which to add it.


Update 2013, as I mention in "searching through .jar files eclipse", it is no longer maintained, and the alternatives are sparse.

As sunleo comments below:

with Eclipse, Ctfl+Shift+T remains the easiest alternative to look for a type
(with the jar name displayed in the status bar).


user862268 comments below:

For mac, it is cmd+shift+T in Eclipse to find the class and associated jar.

Solution 3:

go for Ctrl+Shift+T in Eclipse IDE

Open Type dialog appears where you enter the class name. after that package identifier and jar destination get displayed.

Solution 4:

To answer the question, there is no real way to know which jar to use. Different versions will have potentially different behaviour.

When it comes to locating a jar which contains a given class, I use:

for f in `find . -name '*.jar'`;  do echo $f && jar tvf $f | grep -i $1; done

This will highlight any jar containing the classname passed in as a parameter in any subfolder.

Another good way to find a class is to use the maven repos search.