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条回答
别忘想泡老子
2楼-- · 2019-03-11 00:21

I don't see the necessity for using exec, this worked for me in Cygwin on Windows:

find . -name "*.jar" | while read jarfile; do unzip -l $jarfile | fgrep "foo" ; done 
查看更多
在下西门庆
3楼-- · 2019-03-11 00:21

If you are using Eclipse, you can create a project and add all jars. You will then be able to find a class in any of the jars.

查看更多
爷的心禁止访问
4楼-- · 2019-03-11 00:24

Find jar files and get all classes and members saved on a txt.

(for j in $(find -name '*.jar');do echo "--$j --";jar tf $j | grep .class | sed 's#/#.#g' | sed 's/.class/ /g' | xargs -n 1 javap -classpath  $j ; done;) >> classes.txt
查看更多
放我归山
5楼-- · 2019-03-11 00:25

If you coming to this question and you need to extract all jar files in a windows environment, navigate to the directory where you have saved all the jar files and save a batch file with the following content:

for /r %%f in (*) do jar -xvf %%f

By running the batch file, it will extract each jar file in the directory. The only thing here is that it will extract in the same directory where the jar file is.

查看更多
淡お忘
6楼-- · 2019-03-11 00:29

Here's what I use in Cygwin. It supports section headers per jar file as requested above.

find . -name "*.jar" \
 -exec echo ======\ {}\ ====== \; \
 -exec /c/Program\ Files/Java/jdk1.7.0_45/bin/jar.exe tf {} \; | less

For *nix, drop the ".exe":

find . -name "*.jar" \
 -exec echo ======\ {}\ ====== \; \
 -exec jar tf {} \; | less
查看更多
【Aperson】
7楼-- · 2019-03-11 00:35

You need to pass -n 1 to xargs to force it to run a separate jar command for each filename that it gets from find:

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

Otherwise xargs's command line looks like jar tf file1.jar file2.jar..., which has a different meaning to what is intended.

A useful debugging technique is to stick echo before the command to be run by xargs:

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

This would print out the full jar command instead of executing it, so that you can see what's wrong with it.

查看更多
登录 后发表回答