Abner Ballardo

Have you ever needed to find a jar file which contains a particular java class? If you’re answer is affirmative, please continue reading!

In this short article, I will share one way to do this task in the Unix-like world using sh, ksh or bash. There’re a lot of other ways to do this task so if you have a different one just leave a comment with the code and spread the knowledge!nnLet’s start finding the jar files:

find <dir> -name '*.jar'

Now we need to print all jar files contents and find the java class, the first approach could be:

find <dir> -name '*.jar' | xargs -I @ jar -tvf @ | grep <java class>

We found the java class but we don’t know which jar file it belongs to so let’s try another way:

find <dir> -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep <java class>) done

Great! We can see which jar file is been inspected and when the java class is found it’s printed. Simple, isn’t it? This is just one line that shows the power of unix command shell!

Happy Hacking!