Why Use a Sledgehammer When There is a Bulldozer Lying Around
Or finding which websphere portal shared jar contains the class you need.
When trying to find which WebSphere portal jar file contains the PortletServiceHome class (for the unitiated there are 225 jars in the portal shared directory), I was pushing my very rusty shell scripting skills. I pulled in Doug for some advice, and we were working through various sledgehammer approaches and talking to google about what to do. We were playing with various combinations of pipes and shell commands to get the output of the find (or ls) to play with the jar tvf and grep.
AJ then piped in with just use grep, and he really meant.. just use grep, and take advantage of the fact that the contents of the jar will be listed in plain text in the jar.
The command I ended up with was (running in PortalServer/shared/app):
grep PortletServiceHome *.jar
Which returns:
Binary file wp.pe.api.standard.jar matches
Binary file wp.pe.rt.impl.jar matches
Then running jar -tvf across the first jar shows that this is what I need to compile against and work with when developing.
The bulldozer of a grep across jars is the best way to find which jar contains a class that you are looking for1.
1 - While this feels really inefficient, it is actually better than having to spawn off multiple the jar processes which other approaches would involve. ↩
March 14th, 2008 at 6:02 am
Hi,
I have a similar method to you (also slegehammer-ey) but it works. I have a script called superfind.sh - if I need to know where a class is, I just cd to the top of a likely directory tree and go superfind.sh and it will print out all the classes found.
#!/bin/bash
find . -type f -print0 | xargs -0 grep -l “$1″
August 20th, 2008 at 4:35 am
[…] Old skool java programmers will have almost certainly come across the tool JWhich (http://www.javaworld.com/javaworld/javatips/jw-javatip105.html). It's the classic tool for finding out which jar file contains a class. The following line of code has the same effect, and is slightly better than just trying to grep jar files. […]