Tag: Find all Jars for a Class

Find the Jar File Given a Class Name

March 3, 2011

Often times, while working in Java, you get a ClassNotFoundException or a ClassCastException and you are trying to find find out what Jar the class belongs to and where is it located in the classpath. Your application is either not finding the class or finding the wrong class with the same Class name in the classpath. So you wanna know what Jar is your class coming from at Runtime and whether that is the right class.

Grep to find all Jars with the Class name:

You could write a little bash script to do a find for the class file within your fileSystem, but that doesn’t tell you whats loaded in the classpath. So it will give you shit load of crap that have the class name:

$ cd ~/.groovy
$ $ find . -name "*.jar" -exec sh -c 'jar -tf {} | grep -H --label {} org.apache.commons.httpclient.HttpClient.class' ;
./lib/commons-httpclient-3.1.jar
./lib/commons-httpclient-3.1_patched.jar

This above command will search in the current directory and all sub directories for any jars. Then for each jar file, it will view the contents of the jar file using jar tf and look for the java class org.apache.commons.httpclient.HttpClient.class. The output will be different depending on where I am running the script from.

Java Class to find Jars with the given Class in Classpath:

But you only want to find the Jar File loaded into the Java Classpath. Here’s a simple Java Class that does the same from within an main class:

import java.net.URL;

import org.apache.commons.httpclient.HttpClient;


public class MainApp {
    
    public static void main(String[] args) {
        System.out.println(findPathJar(HttpClient.class));
    }
    
    public static String findPathJar(Class<?> context) throws IllegalStateException {
        URL location = context.getResource('/' + context.getName().replace(".", "/") 
                            + ".class");
        String jarPath = location.getPath();
        return jarPath.substring("file:".length(), jarPath.lastIndexOf("!"));
    }   
}

This will print the jar file in the classpath that contains the class HttpClient.class:

/Users/sacharya/Documents/MyLibs/commons-httpclient-3.1.jar

The output will be same no matter where I am running the class from, since it is looking at the classpath and not the current directory.

Handy Groovy Script to find Jar with the given Class in Classpath:

#!/usr/bin/env groovy

def klass
System.in.withReader {
   println "Enter the Class name you want to find the jar for:"
   klass = it.readLine()
}
def context = Class.forName(klass)
def absolutePath = context.getResource('/' + context.name.replace(".", "/") 
           + ".class").getPath()
println absolutePath.substring("file:".length(), absolutePath.lastIndexOf("!"))

Running this as a script, I get:

$ ./getJarFile.groovy 
Enter the Class name you want to find the jar for:
org.apache.commons.httpclient.HttpClient
/Users/sacharya/.groovy/lib/commons-httpclient-3.1.jar

Again, the output will be same no matter where I am running the script from.

Last of all, I find the site http://jarfinder.com/ very helpful too to find Jar files for a given class.