Perl: Finding a Class in a Jar file.
Posted by jaystile on June 19, 2009
I was having some trouble finding which jar contained a class I needed. I wrote a perl script to find it.
#!/usr/bin/perluse strict;
# flush print to the output after every write.
$| = 1;##############################################################################
# Parse the parameters.
##############################################################################
if( (scalar @ARGV) ne 2) {print "Expected arguments: findClass.pl <ROOT_DIRECTORY> <CLASS_NAME>\n";
exit;
}
print "findClass.pl\n";
my $dir = shift @ARGV;
my $targetClassName = shift @ARGV;
print "Using root directory: $dir\n";
print "Looking for class: $targetClassName\n";##############################################################################
# Backup your current directory, go to working directory
##############################################################################
my $currDir = `pwd`;
chomp $currDir;
chdir $dir;# work
searchDir();##############################################################################
# Return to previous directory and exit.
##############################################################################
chdir $currDir;
exit;##############################################################################
# Subroutines
##############################################################################
sub searchDir {
my @jarList = `find . -name "*.jar"`;
my @classList;
my @results = [];foreach my $jarName (@jarList) {
print ".";
@classList = `jar -tf $jarName`;
foreach my $className (@classList) {
if($className =~ /$targetClassName/) {
print "*";
chomp $jarName;
chomp $className;
push @results, "$jarName:$className";
}
}
}foreach my $result (@results) {
print "$result\n";
}
}
Alexandr Ciornii said
Instead of
my $currDir = `pwd`;
it is better to use Cwd module.