bash 如何在 unix 中查找包含特定类的 jar 文件列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14952678/
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
How to find a list of jar files that contains a particular class in unix
提问by namalfernandolk
I want to find a list of jar files that contains a given , by searching in a particular directory (DIR) and all its sub directories.
我想通过在特定目录 (DIR) 及其所有子目录中搜索来查找包含给定 的 jar 文件列表。
I tried below command. But it provided all the classes that contains the CLASS FILE name.
Eg : If the class is Message.class, following command out put the HttpMessage.classlike class as well.
我试过下面的命令。但它提供了包含 CLASS FILE 名称的所有类。
例如:如果类是Message.class,下面的命令也输出HttpMessage.class类似的类。
find <DIR> -name '*.jar' | while read F; do (echo $F; jar -tvf $F | grep <class>) done - prints the jar name if the class exists then prints the class name.
回答by Wenbing Li
Using grepcan find the matched jar, however, it cannot list the namespace of the class.
usinggrep可以找到匹配的jar,但是不能列出类的命名空间。
Below command combined with findand jarcommands, it will print out the class with package nameand also the jar file name.
下面的命令结合find和jar命令,它将打印出包含包名和jar 文件名的类。
find . -type f -name '*.jar' -print0 | xargs -0 -I '{}' sh -c 'jar tf {} | grep Message.class && echo {}'
You can also search with your package name like below:
您还可以使用您的包名称进行搜索,如下所示:
find . -type f -name '*.jar' -print0 | xargs -0 -I '{}' sh -c 'jar tf {} | grep com/mypackage/Message.class && echo {}'
回答by ipolevoy
Here is a program I developed just for this problem: https://github.com/javalite/jar-explorer
这是我为这个问题开发的一个程序:https: //github.com/javalite/jar-explorer
回答by Symaxion
You can use regexes in grep to specify the exact matching criteria. If you want the line to end with /Message.class, you can do something like this:
您可以在 grep 中使用正则表达式来指定精确的匹配条件。如果您希望该行以/Message.class 结尾,您可以执行以下操作:
grep '/Message.class$'
grep '/Message.class$'

