How to search for a string in JAR files

Solution 1:

You can use zipgrep on Linux or OSX:

zipgrep "BEGIN REQUEST" file.jar

If you wish to search a number of jars, do

find libdir -name "*.jar" -exec zipgrep "BEGIN REQUEST" '{}' \;

where libdir is a directory containing all jars. The command will recursively search subdirectories too.

For windows, you can download cygwin and install zipgrep under it: http://www.cygwin.com/

Edit 1

To view the name of the file that the expression was found you could do,

find libdir -name "*.jar" | xargs -I{} sh -c 'echo searching in "{}"; zipgrep "BEGIN REQUEST" {}'

Solution 2:

Caution: This is not an accurate answer, it's only a quick heuristic approach. If you need to find something like the name of a class (e.g., which jar has class Foo?) or maybe a method name, then this may work.

grep --text 'your string' your.jar

This will search the jar file as if it were text. This is quicker because it doesn't expand the archive, but that is also why it is less accurate. If you need to be exhaustive then this is not the approach you should use, but if you want to try something a little quicker before pulling out zipgrep this is a good approach.


From man grep,

  -a, --text
          Process  a binary file as if it were text; this is equivalent
          to the --binary-files=text option.

Solution 3:

in android i had to search both jar and aar files for a certain string i was looking for here is my implementation on mac:

find . -name "*.jar" -o -name "*.aar" | xargs -I{} zipgrep "AssestManager" {}

essentially finds all jars and aar files in current direclty (and find command is recursive by default) pipes the results to zipgrep and applies each file name as a parameter via xargs. the brackets at the end tell xargs where to put the file name you got from the find command. if you want to search the entire home directory just change the find . to find ~