What jar is that friggin class in?!?!?!
March 21, 2009
Leave a comment
It seems to me that one of the most common tasks I have to preform when packaging a J2EE project is to find out what jar a class I depend on is in.
I have written (multiple times now) a simple perl script that finds all the jars in a given directory and scans them for a file. Thanks to the power of regular expressions you can pass in either com.foo.bar.Class or com/foo/bar/Class which ever is easier to cut-n-paste from the java.lang.NoClassDefFoundError, or similar, stack trace.
#!/usr/bin/perl
$JAR_COMMAND = "jar";
#Uncomment to use fastjar instead
#$JAR_COMMAND = "fastjar";
$class = "";
$lookin = ".";
if(@ARGV < 1) {
print "USAGE: \n\tclassfinder [dir to search fo jars]
<packageName>\n";
exit;
} elsif (@ARGV > 1) {
$lookin = $ARGV[0];
$class = $ARGV[1];
} else {
$class = $ARGV[0];
}
foreach $jar (`find $lookin -name '*\.jar'`) {
chomp $jar;
foreach $file (`$JAR_COMMAND -tf $jar`) {
chomp $file;
if($file =~ $class) {
print "$jar:$file\n"}
}
}
The script relies on the standard unix utility find, it works on unix, linux, osx and cygwin. If you have an easy way to do this without using find I’d love to see it.