[Logo]
Forums Register Login
bash command to know .jar of searched class
hi,

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?

thanks!
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
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-10.2.2.0.jar 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).
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!).
Since jars are zipped, and zgrep is a combination of zip and grep, you may use zgrep:

I use jarFinder, and find it indispensable.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Sep 21, 2017 07:00:39.