views:

1158

answers:

7

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 appserver we need to write a small shell scripts that prints out jars in which this specific class is found.

A: 

Now to answer this question here is a simple shell command that did that for us.

for jarFile in $(
    ls -R | 
    awk '
        match($0, ".*:") { 
            folder=$0 
        } 
        ! match($0, ".*:") {
            print folder$0 
        }' | 
        grep "\.jar" | 
        sed -e "s/:/\//g"
); 
do  
    unzip -l $jarFile; 
done | 
awk '
    match($0, "Archive.*") { 
        jar=$0 
    } 
    ! match($0, "Archive.*") {
        print jar" : "$0 
    }' | 
grep org.jboss.Main
Irfan Zulfiqar
+1  A: 

If you need result only then you can install agentransack http://www.mythicsoft.com/agentransack/ and do a containing text search. Agentransack searches inside jar and zip files as well.

Bhushan
A: 

Years ago I wrote a utility classfind to resolve issues like this. Set your classpath to point to your .jar set, and classfind will tell you in which jars it'll find a particular class.

example% classfind -c Document
/usr/java/j2sdk1.4.0/jre/lib/rt.jar -> org.w3c.dom.Document
/usr/java/j2sdk1.4.0/jre/lib/rt.jar -> javax.swing.text.Document
Brian Agnew
+3  A: 

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.

Bartosz Klimek
+5  A: 

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

araqnid
Using 'unzip -l' instead for 'jar' made it run very very fast. 'jar' took 72 seconds vs 4 second when run with 'unzip -l'
Irfan Zulfiqar
I came in here to say exactly this. +1
Ian McLaird
If I want to know in which jar a certain class can be found I usually do something like cd ~/.m2/repository; grep -r -H "classname" *.
extraneon
A: 

This tool is pretty useful: http://code.google.com/p/jar-explorer/wiki/HowToInstall

This pops open a gui window with two panels, you can pick a directory, the tool will scan all the jars 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 jar files scanned.

A: 

Hi,

you may also want to have a look at this java tool called jarscan: https://jarscan.dev.java.net

One can search by class name and by package.

Helped a lot so far where Total Commander wasn't available and only java was allowed to execute.

cheers, Michael

marksml