Linux 列出多个jar文件的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7770183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:42:16  来源:igfitidea点击:

List contents of multiple jar files

javalinuxshelljar

提问by user443854

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

我正在一堆罐子中搜索 .class 文件。

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.

什么都不打印。我能想到的唯一解决方案是全部解压缩,然后搜索。有没有更好的办法?我在 LUnix 上。

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

编辑:扫描许多 jar 时,将 jar 文件名与类一起打印很有用。这种方法效果很好:

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.class
  1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.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.class
  1718 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class
 42607 Wed Jul 25 10:20:18 EDT 2007 org/apache/ws/commons/schema/SchemaBuilder.class
./XmlSchema.jar

采纳答案by NPE

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

您需要传递-n 1toxargs以强制它为jar它从中获取的每个文件名运行单独的命令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.

否则xargs的命令行看起来像jar tf file1.jar file2.jar...,它与预期的含义不同。

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

一个有用的调试技术是坚持echo在要运行的命令之前xargs

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

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

这将打印出完整的jar命令而不是执行它,以便您可以看到它有什么问题。

回答by Erel

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.

如果您使用的是 Eclipse,您可以创建一个项目并添加所有 jar。然后,您将能够在任何 jar 中找到一个类。

回答by Jaime Soriano

You can also use -execoption of find

您还可以使用-exec查找选项

find . -name "*.jar" -exec jar tf {} \;

回答by Laurent Legrand

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

您还可以使用比 jar 更快的 unzip(用于打印 zip 内容的 -l 选项)。

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

回答by Ryan

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

这是我在 Cygwin 中使用的内容。它支持上述要求的每个 jar 文件的节头。

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

For *nix, drop the ".exe":

对于 *nix,删除“.exe”:

find . -name "*.jar" \
 -exec echo ======\ {}\ ====== \; \
 -exec jar tf {} \; | less

回答by Praveen Baskar

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.

我遇到了类似的情况,我需要在目录中存在的 jar 文件列表中搜索一个类。我也想知道该类属于哪个 jar 文件。我使用了下面的代码片段(shell 脚本)并发现它很有帮助。下面的脚本将列出包含要搜索的类的 jar 文件。

#!/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

Shell 脚本取自:http: //alvinalexander.com/blog/post/java/shell-script-search-search-multiple-jar-files-class-pattern

回答by astrograph

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

我认为没有必要使用 exec,这在 Windows 上的 Cygwin 中对我有用:

find . -name "*.jar" | while read jarfile; do unzip -l $jarfile | fgrep "foo" ; done 

回答by DiegoRG

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

查找 jar 文件并将所有类和成员保存在 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

回答by Donia Zaela

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:

如果遇到这个问题,需要在windows环境下解压所有jar文件,导航到保存所有jar文件的目录,保存一个批处理文件,内容如下:

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.

通过运行批处理文件,它将提取目录中的每个 jar 文件。这里唯一的事情是它将提取到 jar 文件所在的同一目录中。