Linux cmd 在 jar 中搜索类文件,而不考虑 jar 路径 [英] Linux cmd to search for a class file among jars irrespective of jar path
问题描述
我想在许多 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.jar
和foo/b/b.jar
都在foo/
文件夹下,在这种情况下,您可以将 find
与 grep
一起使用:
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 会将查找 jar 文件的路径存储在像 CLASS_PATH
这样的环境变量中,我不知道这是否是您想要的.但是如果你的变量是这样的: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屋!