Find All Jars Containing Given Class – 查找包含给定类别的所有罐子

最后修改: 2022年 1月 16日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this article, we’ll learn to find all the jars containing a particular class. We’ll demonstrate this using two different approaches, namely, command-based and program-based.

在这篇文章中,我们将学习如何找到包含一个特定类的所有罐子。我们将使用两种不同的方法进行演示,即基于命令和基于程序。

2. Command Based

2.基于命令的

In this approach, we’ll use the shell command to identify all the jars in the local maven repository that have the ObjectMapper class. Let’s start by writing a script to identify the class in a jar. The script uses the jar and grep command to print the appropriate jar:

在这种方法中,我们将使用shell命令来识别本地maven仓库中拥有ObjectMapper类的所有jar。让我们先写一个脚本来识别jar中的类。该脚本使用jargrep命令来打印相应的jar。

jar -tf $1 | grep $2 && echo "Found in : $1"

Here $1 is the jar file path, and $2 is the class name. The class name will always be com.fasterxml.jackson.databind.ObjectMapper for this scenario. Let’s save the above command in a bash file findJar.sh. After that, we’ll run the following find command on the local maven repository, with findJar.sh to get the resultant jars:

这里 1 是 jar 文件路径,2 是类的名称。在这种情况下,类名将总是com.fasterxml.jackson.databind.ObjectMapper。让我们把上述命令保存在一个 bash 文件中 findJar.sh。之后,我们将在本地maven仓库上运行以下find命令,用findJar.sh得到结果的jars:

$ find ~/.m2/repository -type f -name '*.jar' -exec ./findJar.sh {} com.fasterxml.jackson.databind.ObjectMapper \;
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper$1.class
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper$2.class
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper$3.class
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper$DefaultTypeResolverBuilder.class
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper$DefaultTyping.class
com/spotify/docker/client/shaded/com/fasterxml/jackson/databind/ObjectMapper.class
Found in : <strong>/home/user/.m2/repository/com/spotify/docker-client/8.16.0/docker-client-8.16.0-shaded.jar>
com/fasterxml/jackson/databind/ObjectMapper$1.class
com/fasterxml/jackson/databind/ObjectMapper$2.class
com/fasterxml/jackson/databind/ObjectMapper$3.class
com/fasterxml/jackson/databind/ObjectMapper$DefaultTypeResolverBuilder.class
com/fasterxml/jackson/databind/ObjectMapper$DefaultTyping.class
com/fasterxml/jackson/databind/ObjectMapper.class
Found in : <strong>/home/user/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.3/jackson-databind-2.12.3.jar>

3. Program Based

3.基于计划

In the program-based approach, we’ll write a Java class to find the ObjectMapper class in the java classpath. We can display the jar as shown below program:

在基于程序的方法中,我们将编写一个Java类,在java classpath中找到ObjectMapper类。我们可以像下面的程序那样显示jar。

public class App { 
    public static void main(String[] args) { 
        Class klass = ObjectMapper.class; 
        URL path = klass.getProtectionDomain().getCodeSource().getLocation(); 
        System.out.println(path); 
    } 
}

Output:

输出。

file:/Users/home/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.12.3/jackson-databind-2.12.3.jar

Here we see every Class class have getProtectionDomain().getCodeSource().getLocation(). This method provides the jar file where the desired class exists. Therefore, we can use it to get the jar file having the class.

这里我们看到每个Class类都有getProtectionDomain().getCodeSource().getLocation()/em>。这个方法提供了所需类存在的jar文件。因此,我们可以用它来获取拥有该类的jar文件。

4. Conclusion

4.总结

In this article, we’ve learned command and program-based approaches to find classes from the jars list.

在这篇文章中,我们已经学习了基于命令和程序的方法来从jars列表中寻找类。

Firstly we started with an illustrative example. After that, we explored a command-based approach to identify a given class from the local maven repository. And then, in the second approach, we learned to write a program to find the jar used in runtime from the classpath to instantiate the class.

首先,我们从一个说明性的例子开始。之后,我们探索了一种基于命令的方法,从本地maven资源库中识别一个给定的类。然后,在第二种方法中,我们学会了写一个程序,从classpath中找到运行时使用的jar,以实例化该类。

Both methods are effective, but they have their own use case.

这两种方法都很有效,但它们有各自的使用情况。