Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Introduction

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

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:

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:

$ 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</strong>
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</strong>

3. Program Based

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:

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.

4. Conclusion

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

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.

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

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!