Saturday, October 23, 2010

How to find which .jar a class is in (easily)

Along with the many blessings a high level language brings you are a few curses. In Java's case, one of the biggest downfalls is the dependency graph your code becomes part of as you develop applications. This is especially noticeable when you're using frameworks like JEE or Spring.

A Java developer feels this pain in a couple of ways:

- Compile time, manifested with errors such as "(ClassName) cannot be resolved to a Type" or "The import some.class.SoughtFor cannot be resolved".

- Runtime, where the errors will show themselves with messages like the all-time favorite "java.lang.ClassNotFoundException".

But what if you had a report like this? Wouldn't that be nice?

So what do you do then? Many coders start by trying to guess which .jars hold the missing classes, then add them to the classpath one by one. This can be an exercise in frustration, as often times .jar names give you little clue as to which classes are inside.

A second methodology developers sometimes use is the 'throw everything but the kitchen sink at it' approach, where they build huge classpaths loaded with every .jar they can find. This inexact coping mechanism obviously leads to bloat.

But wait! There is an exact way to cure the problem, and it's easy and open sourced! Here's how to do it:

1) Download and unzip JBoss Tattletale. It can be found here.
2) Make a run script for Tattletale.
3) Run the run script.
4) Read the report Tattletale makes for you. It's that easy!

The script is easy to write. It takes the form 'java -jar tattletale.jar DIRECTORY_TO_RECURSIVELY_SEARCH DIRECTORY_FOR_REPORTS.

Here's an example:
java -jar tattletale.jar /home/rick/Tools/JBoss/jboss-5.1.0.GA /home/rick/rpts

Then you just go to the reports directory and view the index. To find your classes in .jars, have a look at the 'Class Location' report. It'll tell you which jar holds the missing classes!

Tattletale has many more capabilities, which are well documented in the packaged docs. I hope you'll find this tool useful in your day-to-day development work.

Happy Coding!


Anonymous said...

I use the "java class finder" plugin for eclipse. It's very simple and makes finding your classes a piece of cake.

Anonymous said...

or use a small bash script

for j in `find . -name '*.jar' -type f`; do
for c in `jar tf $j | grep $1`; do
echo "$j : $c";

Siddique said...

Library Finder helps you finding jar files which has .class, resources (.properties, .xml etc.) -
Support regex, wilcard, case sensitivity matching etc.

It is available as IntelliJ plugin and also runs on command line mode.

Vivek said...

I had written classjarsearch a simple utility for the same. Does the job for me.

Fabien Taysse said...

"ctrl+shift+T" in Eclipse can give you the jar(s) in your project's classpath containing the class
If the jar is not in the classpath of your project, is your best friend

Evgeny Goldin said...

Another option is to use a Total Commander and search inside *.jar files as if they were plain text files:

kininwong said...

search using winrar