Finding a needle.class in (multiple) haystack.jar


Sometimes, you get an error like this:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Maps
at sample.contacts.ContactsExample.<init>(
at sample.contacts.ContactsExample.main(

It means you’re missing a class, specifically

Let’s say you download a package and get a million jars. How do you know if that class is in the jars and which one?

Try this linux script:

for i in *.jar; do jar -tvf "$i" | fgrep Maps; done

(note the quotes need to be regular quotes, and not the stylized ones WordPress imposes on me here)

If it’s found, you can print out all the classes in the jars into a text file and scan through it for the jar that contains your class

for i in *.jar;
  do echo "$i" >> myjarcontents.txt;
  jar -tvf "$i" >> myjarcontents.txt;
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: