JarScan, scan all JAR files in all subfolders for

2019-02-03 13:57发布

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?

标签: java class jar
8条回答
趁早两清
2楼-- · 2019-02-03 14:10

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
查看更多
Evening l夕情丶
3楼-- · 2019-02-03 14:12
#! /bin/sh

path=$1
segment=$2

if [ -z "$path" ] || [ -z "$segment"  ]
then 
    echo "Usage: $0 <path> <segment>"
    exit
fi

for jar in $path/*.jar ; do echo " *** $jar *** " ; jar tf $jar| grep --color=always $segment; done;
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-03 14:13

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.

查看更多
▲ chillily
5楼-- · 2019-02-03 14:15

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.

查看更多
可以哭但决不认输i
6楼-- · 2019-02-03 14:21

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

查看更多
7楼-- · 2019-02-03 14:21

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
查看更多
登录 后发表回答