Linux cmd 在 jars 中搜索类文件,而不管 jar 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14373788/
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
Linux cmd to search for a class file among jars irrespective of jar path
提问by AlwaysALearner
I want to search for a particular class file among many jar files without giving the location of each jar file.
我想在许多 jar 文件中搜索特定的类文件,而不提供每个 jar 文件的位置。
Is this possible with a simple command?
这可以通过简单的命令实现吗?
I tried this command:
我试过这个命令:
grep Hello.class *.jar
Which did not return a list of jars containing the class. Then I ran the command:
其中没有返回包含该类的 jar 列表。然后我运行了命令:
grep Hello.class /full/path/of/jar/file/*.jar
Which did return the relevant jar file. Is there a better way?
哪个确实返回了相关的 jar 文件。有没有更好的办法?
采纳答案by Bin Wang
Where are you jar files? Is there a pattern to find where they are?
你在哪里 jar 文件?有没有一种模式可以找到它们的位置?
1. Are they all in one directory?
1. 它们都在一个目录中吗?
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
:
例如,foo/a/a.jar
并且foo/b/b.jar
都在文件夹下foo/
,在这种情况下,您可以使用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.
当然,至少你可以在根目录下搜索它们/
,但是会很慢。
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:
正如@loganaayahee 所说,您也可以使用命令locate
. locate
用索引搜索文件,这样会更快。但命令应该是:
locate "*.jar" | xargs grep Hello.class
Since you want to search the content of the jar files.
由于您要搜索 jar 文件的内容。
2. Are the paths stored in an environment variable?
2. 路径是否存储在环境变量中?
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:
通常,Java 会将查找 jar 文件的路径存储在一个环境变量中,例如CLASS_PATH
,我不知道这是否是您想要的。但是如果你的变量是这样的: CLASS_PATH=/lib:/usr/lib:/bin
,它使用 a:
来分隔路径,那么你可以使用这个命令来搜索类:
for P in `echo $CLASS_PATH | sed 's/:/ /g'`; do grep Hello.calss $P/*.jar; done
回答by loganaayahee
locate *.jar | grep Hello.class.jar
The locate command to search the all path of the particular file and display full path of the file.
locate 命令用于搜索特定文件的所有路径并显示文件的完整路径。
example
例子
locate *.jar | grep classic.jar
/opt/ltsp/i386/usr/share/firefox/chrome/classic.jar
/opt/ltsp/i386/usr/share/thunderbird/chrome/classic.jar
/root/.wine/drive_c/windows/gecko/0.1.0/wine_gecko/chrome/classic.jar
/usr/lib/firefox-3.0.14/chrome/classic.jar
/usr/lib/firefox-3.5.2/chrome/classic.jar
/usr/lib/xulrunner-1.9.1.2/chrome/classic.jar
/usr/share/firefox/chrome/classic.jar
/usr/share/thunderbird/chrome/classic.jar
回答by Wenbing Li
Most of the solutions are directly using grep
command to find the class. However, it would not give you the package name of the class. Also if the jar is compressed, grep
will not work.
大多数解决方案都是直接使用grep
命令来查找类。但是,它不会为您提供类的包名称。此外,如果 jar 被压缩,grep
将无法工作。
This solution is using jar
command to list the contents of the file and grep
the class you are looking for.
此解决方案使用jar
命令列出文件的内容和grep
您要查找的类。
It will print out the class with package nameand also the jar file name.
它将打印出包含包名和jar 文件名的类。
find . -type f -name '*.jar' -print0 | xargs -0 -I '{}' sh -c 'jar tf {} | grep Hello.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/Hello.class && echo {}'
回答by meliniak
Use deepgrep and deepfind. On debian-based systems you can install them by:
使用 deepgrep 和 deepfind。在基于 debian 的系统上,您可以通过以下方式安装它们:
sudo apt-get install strigi-utils
Both commands search for nested archives as well. In your case the command would look like:
这两个命令也搜索嵌套档案。在您的情况下,命令如下所示:
find . -name "*.jar" | xargs -I {} deepfind {} | grep Hello.class
回答by Eric Leschinski
Linux, Walkthrough to find a class file among many jars.
Linux,在许多 jar 中找到一个类文件的演练。
Go to the directory that contains the jars underneath.
转到包含下面 jar 的目录。
eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ ls
blah.txt metrics-core-2.2.0.jar
jopt-simple-3.2.jar scala-library-2.10.1.jar
kafka_2.10-0.8.1.1-sources.jar zkclient-0.3.jar
kafka_2.10-0.8.1.1-sources.jar.asc zookeeper-3.3.4.jar
log4j-1.2.15.jar
I'm looking for which jar provides for the Producer class.
我正在寻找为 Producer 类提供的 jar。
Understand how the for loop works:
了解 for 循环的工作原理:
eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `seq 1 3`; do
> echo $i
> done
1
2
3
Understand why find this works:
了解为什么 find 这有效:
eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ find . -name "*.jar"
./slf4j-api-1.7.2.jar
./zookeeper-3.3.4.jar
./kafka_2.10-0.8.1.1-javadoc.jar
./slf4j-1.7.7/osgi-over-slf4j-1.7.7-sources.jar
You can pump all the jars underneath into the for loop:
您可以将下面的所有罐子泵入 for 循环:
eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `find . -name "*.jar"`; do
> echo $i
> done
./slf4j-api-1.7.2.jar
./zookeeper-3.3.4.jar
./kafka_2.10-0.8.1.1-javadoc.jar
./kafka_2.10-0.8.1.1-sources.jar
Now we can operate on each one:
现在我们可以对每一个进行操作:
Do a jar tf
on every jar and cram it into blah.txt:
jar tf
在每个 jar 上做一个,然后把它塞进 blah.txt:
for i in `find . -name "*.jar"`; do echo $i; jar tf $i; done > blah.txt
Inspect blah.txt, it's a list of all the classes in all the jars. You can search that file for the class you want, then look for the jar that came before it, that's the one you want.
检查 blah.txt,它是所有 jars 中所有类的列表。您可以在该文件中搜索您想要的类,然后查找它之前的 jar,这就是您想要的那个。
回答by tk_
Just go to the directory which contains jars and insert below command:
只需转到包含 jars 的目录并插入以下命令:
find *.jar | xargs grep className.class
Hope this helps!
希望这可以帮助!
回答by user6319683
eric@dev /home/el/kafka_2.10-0.8.1.1/libs $ for i in `find . -name "*.jar"`; do
> echo $i
> done
./slf4j-api-1.7.2.jar
./zookeeper-3.3.4.jar
./kafka_2.10-0.8.1.1-javadoc.jar
./kafka_2.10-0.8.1.1-sources.jar
回答by aayush singhal
I have used this small snippet. Might be slower but works every time.
我用过这个小片段。可能会更慢,但每次都有效。
for i in 'find . -type f -name "*.jar"'; do
jar tvf $i | grep "com.foo.bar.MyClass.clss";
if [ $? -eq 0 ]; then echo $i; fi;
done