Linux cmd在jar中搜索一个类文件,而不考虑jar路径。

时间:2022-03-20 18:42:32

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文件。有更好的方法吗?

8 个解决方案

#1


70  

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

jar文件在哪里?有没有一种模式可以找到它们的位置?

1. Are they all in one directory?

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 / /。jar和foo / b / b。jar都在foo/文件夹下,在这种情况下,可以使用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 "*.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?

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会在CLASS_PATH这样的环境变量中存储找到jar文件的路径,我不知道这是否是您想要的。但是,如果您的变量是这样的:CLASS_PATH=/lib:/usr/lib:/bin,使用a:分隔路径,那么您可以使用这个推荐来搜索类:

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

#2


25  

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 name and 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 {}'

#3


4  

Just go to the directory which contains jars and insert below command:

只需进入包含jar的目录,并在命令下面插入:

find *.jar | xargs grep className.class

Hope this helps!

希望这可以帮助!

#4


2  

   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

#5


1  

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

#6


1  

Linux, Walkthrough to find a class file among many jars.

在许多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.

我正在寻找jar为Producer类提供的内容。

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:

理解为什么要找到这样的工作:

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:

在每个罐子上做一个罐子tf,然后塞进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.

检查等等。txt,它是所有jar中的所有类的列表。你可以搜索那个文件,找到你想要的类,然后找到它之前的jar,这就是你想要的。

#7


0  

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

#8


-1  

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

#1


70  

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

jar文件在哪里?有没有一种模式可以找到它们的位置?

1. Are they all in one directory?

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 / /。jar和foo / b / b。jar都在foo/文件夹下,在这种情况下,可以使用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 "*.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?

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会在CLASS_PATH这样的环境变量中存储找到jar文件的路径,我不知道这是否是您想要的。但是,如果您的变量是这样的:CLASS_PATH=/lib:/usr/lib:/bin,使用a:分隔路径,那么您可以使用这个推荐来搜索类:

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

#2


25  

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 name and 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 {}'

#3


4  

Just go to the directory which contains jars and insert below command:

只需进入包含jar的目录,并在命令下面插入:

find *.jar | xargs grep className.class

Hope this helps!

希望这可以帮助!

#4


2  

   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

#5


1  

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

#6


1  

Linux, Walkthrough to find a class file among many jars.

在许多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.

我正在寻找jar为Producer类提供的内容。

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:

理解为什么要找到这样的工作:

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:

在每个罐子上做一个罐子tf,然后塞进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.

检查等等。txt,它是所有jar中的所有类的列表。你可以搜索那个文件,找到你想要的类,然后找到它之前的jar,这就是你想要的。

#7


0  

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

#8


-1  

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