File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes bash command to know .jar of searched class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "bash command to know .jar of searched class" Watch "bash command to know .jar of searched class" New topic

bash command to know .jar of searched class

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308

sometimes i come across the problem that a NoClassDefFoundError occurs and I don't know the jar for it. Searching manually is cumbersome, so I want to find out by bash command.

Following gives me a list whether it could be found out by a list of jars, but it does not give me the information which jar it is:

find . -name "*jar" -exec jar -tf '{}' \; | grep ClassToSearchFor

is it possible to place a echo output of jar-filename in the above find command also?


aldana software engineering blog & .more
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

Possibly the easiest way is to search for just the unqualified class name in the jar file itself (so don't try to give it any package names, and don't try to list the contents of the jar file). Assuming you don't have too many files to search through, you grep for the class name in the list of jar files:

If there are too many files to list on the command line in that way, you could try a much slower method such as looking at each file individually:

Of course we can then start making the commands even more complex to show exactly which class we found (in the cases where there may be multiple matches), and expand our search to include wars, ears, sars,...:

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
How about:where you replace CLASS_NAME with the class name? For example:would give you a list like this:so you know the VTITemplate class resides in the derby- file. That's quite efficient, and for small numbers of JARs quite readable too.

You could be more sophisticated with grep or sed to parse that output further if you needed to be (e.g. to only output lines ending .jar which come before a line ending .class).

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Or this:which is better. I haven't tested it thoroughly, but you'll get output which is the JAR name followed by all matching names in that JAR. There are undoubtedly shorter and more efficient ways to do this---the above just came out of my head (and therefore, could be dodgy, so use carefully!).
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Since jars are zipped, and zgrep is a combination of zip and grep, you may use zgrep:
Peter Johnson

Joined: May 14, 2008
Posts: 5852

I use jarFinder, and find it indispensable.

JBoss In Action
I agree. Here's the link:
subject: bash command to know .jar of searched class
It's not a secret anymore!