Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JarScan, scan all JAR files in all subfolders for specific class

Tags:

java

class

jar

We are seeing an older version of a class being used, although we had the latest one deploy. To scan all JAR files in all subfolders of an application server, how do we write a small shell script that prints out the file name of JARS files in which this specific class is found?

like image 947
Irfan Zulfiqar Avatar asked Apr 17 '09 10:04

Irfan Zulfiqar


3 Answers

Something like:

find . -name '*.jar' | while read jarfile; do if jar tf "$jarfile" | grep org/jboss/Main; then echo "$jarfile"; fi; done

You can wrap that up like this:

jarscan() {
  pattern=$(echo $1 | tr . /)
  find . -name '*.jar' | while read jarfile; do if jar tf "$jarfile" | grep "$pattern"; then echo "$jarfile"; fi; done
}

And then jarscan org.jboss.Main will search for that class in all jar files found under the current directory

like image 98
araqnid Avatar answered Sep 27 '22 17:09

araqnid


Not directly answering your question, but maybe this will solve your problem: you can print out the location (e.g. the jar file) from which a specific class was loaded by adding a simple line to your code:

System.err.println(YourClass.class.getProtectionDomain().getCodeSource());

Then you will know for sure where it comes from.

like image 29
Bartosz Klimek Avatar answered Sep 27 '22 17:09

Bartosz Klimek


The tool JAR Explorer is pretty useful.

It pops open a GUI window with two panels. You can pick a directory, the tool will scan all the JAR files in that directory, then let you search for a specific class. The lower panel then lights up with a list of hits for that class in all the scanned JAR files.

like image 23
user98960 Avatar answered Sep 27 '22 18:09

user98960