Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to find which jar contain a class, I wrote the below, which doesn't seem to work any ideas?

 find -type f -name "*.jar" -exec jar -tvf "{}" \| grep  MyClass \; -ls
share|improve this question

That doesn't work, because find calls exec(3) directly, and as such, does not interpret the command line as a shell does (to set up the pipe).

When I need to do something like this, I generally toss the stuff I want piped into a script, so that I can do -exec script {} \;. Another option is

find . -type f -name "*.jar" -exec sh -c "jar -tvf '{}' | grep  MyClass" \; -ls

FWIW, my personal findClassInJar is

 for x in `find . -name "*.jar" -o -name "*.zip"` ; do if unzip -l $x | grep -q $1 > /dev/null ; then echo $x ; fi ; done

It won't work with directories with spaces in them, but I just don't do that in a project.

share|improve this answer

I am not quite sure what you are wanting... Here is a bit of bash that displays the filename of a .jar file containing a .class file.

for f in `find *.jar`; do if jar -tvf $f | grep class &>/dev/null; then echo $f; fi; done

You can remove the echo $f statement and put your own customization there, as well as customizing the grep command in it.

Hope it helps.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .