Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a string in JAR files

Tags:

java

jar

My application is built on Java EE.

I have approximately 50 jars in this application.

Is it possible to search for a particular keyword (actually I want to search for a keyword BEGIN REQUEST)?

like image 472
user1253847 Avatar asked Apr 26 '12 09:04

user1253847


People also ask

How do I search a JAR file in Linux?

To find the . jar files that contain a class, you can use the FindClass.sh script. First go to a UNIX installation of Sterling Platform/MCF. If the FindClass.sh script already exists it should be in your $YFS_HOME directory or your $YFS_HOME/lib directory.

How do I search for a text in a class file in eclipse?

Ctrl + H will bring up a Search/Find dialog. One of the tabs is Java which will let you specify what exactly you are looking for e.g. Class/MNethod etc.


2 Answers

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" {}' 
like image 85
Kalpak Gadre Avatar answered Sep 19 '22 02:09

Kalpak Gadre


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. 
like image 29
Captain Man Avatar answered Sep 19 '22 02:09

Captain Man