Linux cmd可以在jar中搜索类文件,而与jar路径无关 [英] Linux cmd to search for a class file among jars irrespective of jar path

查看:209
本文介绍了Linux cmd可以在jar中搜索类文件,而与jar路径无关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在许多jar文件中搜索特定的类文件,而不给出每个jar文件的位置.

I want to search for a particular class file among many jar files without giving the location of each jar file.

用一个简单的命令就能做到吗?

Is this possible with a simple command?

我尝试了以下命令:

grep Hello.class *.jar

没有返回包含该类的jar的列表.然后我运行命令:

Which did not return a list of jars containing the class. Then I ran the command:

grep Hello.class /full/path/of/jar/file/*.jar

哪个确实返回了相关的jar文件.有更好的方法吗?

Which did return the relevant jar file. Is there a better way?

推荐答案

您在哪里jar文件?有没有找到他们所在位置的模式?

Where are you jar files? Is there a pattern to find where they are?

例如,foo/a/a.jarfoo/b/b.jar都在文件夹foo/下,在这种情况下,您可以将findgrep结合使用:

For example, foo/a/a.jar and foo/b/b.jar are all under the folder foo/, in this case, you could use find with grep:

find foo/ -name "*.jar" | xargs grep Hello.class

当然,至少您可以在根目录/下搜索它们,但这会很慢.

Sure, at least you can search them under the root directory /, but it will be slow.

正如@loganaayahee所说,您也可以使用命令locate. locate使用索引搜索文件,这样会更快.但是命令应该是:

As @loganaayahee said, you could also use the command locate. locate search the files with an index, so it will be faster. But the command should be:

locate "*.jar" | xargs grep Hello.class

因为您要搜索jar文件的内容.

Since you want to search the content of the jar files.

通常,Java将在诸如CLASS_PATH之类的环境变量中存储用于查找jar文件的路径,我不知道这是否是您想要的.但是,如果您的变量是这样的:CLASS_PATH=/lib:/usr/lib:/bin,它使用:分隔路径,那么您可以使用此命令来搜索类:

Typically, Java will store the paths to find jar files in an environment variable like CLASS_PATH, I don't know if this is what you want. But if your variable is just like this:CLASS_PATH=/lib:/usr/lib:/bin, which use a : to separate the paths, then you could use this commend to search the class:

for P in `echo $CLASS_PATH | sed 's/:/ /g'`; do grep Hello.calss $P/*.jar; done

这篇关于Linux cmd可以在jar中搜索类文件,而与jar路径无关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆