This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

bash command to know .jar of searched class

 
manuel aldana
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since jars are zipped, and zgrep is a combination of zip and grep, you may use zgrep:

 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use jarFinder, and find it indispensable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic