Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.