List contents of multiple jar files

2019-03-10 23:56发布

I am searching for a .class file inside a bunch of jars.

jar tf abc.jar 

works for one file. I tried

find -name "*.jar" | xargs jar tf

prints nothing. The only solution I can think of, is unzip all, then search. Is there a better way? I'm on LUnix.

Edit: When scanning many jars, it is useful to print the jar file name along with the class. This method works well:

find . | grep jar$ | while read fname; do jar tf $fname | grep SchemaBuilder && echo $fname; done

Sample output produced:

  1572 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$1.class
  1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$2.class
 42607 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class
./XmlSchema-1.3.2.jar
  1572 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$1.class
  1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder$2.class
 42607 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class
./XmlSchema.jar

9条回答
ら.Afraid
2楼-- · 2019-03-11 00:36

I faced a similar situation where I need to search for a class in a list of jar files present in a directory. Also I wanted to know from which jar file the class belongs to. I used this below code snippet(shell script) and found out to be helpful. Below script will list down the jar files that contains the class to be searched.

#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
  echo "Looking in $i ..."
  jar tvf $i | grep $LOOK_FOR > /dev/null
  if [ $? == 0 ]
  then
    echo "==> Found \"$LOOK_FOR\" in $i"
  fi
done

Shell script taken from : http://alvinalexander.com/blog/post/java/shell-script-search-search-multiple-jar-files-class-pattern

查看更多
再贱就再见
3楼-- · 2019-03-11 00:36

You can also use -exec option of find

find . -name "*.jar" -exec jar tf {} \;
查看更多
老娘就宠你
4楼-- · 2019-03-11 00:38

You can also use unzip which is more faster than jar (-l option to print zip content).

For instance, a small script that does roughly what you want:

#!/bin/bash

for i in $(find . -name "*.jar")
do
    if [ -f $i ]
then
    echo $i
    unzip -l $i | grep $*
fi
done
查看更多
登录 后发表回答