Searches for classes in a set of jar files that match a given pattern.
Usage:
jfind -help-s <pattern> <jars ...>
Class <pattern> could contain /.
E.g.: javax/ejb/EJBObject
the script will convert all / to .
Usage Examples:
jfind digester *.jar
jfind -s digester *.jar
Search classpath...
jfind org.foo.Bar /usr/lib/foo.jar:/usr/lib/bar.jar
#!/usr/bin/perl
# author: vladb
# date: 2004-09
#
# Searches for classes in a set of jar files
# that match a given pattern.
use Getopt::Long;
use Data::Dumper;
use strict;
##---------------------------------------------
## CONFIG
##---------------------------------------------
@cfg::options = ("help|h","s");
$cfg::unzip = "/usr/5bin/unzip";
##---------------------------------------------
## MAIN
##---------------------------------------------
my %opts;
GetOptions(\%opts, @cfg::options);
help() if ($opts{help});
my $pattern = shift;
# replace all / with .
$pattern =~ s/\//\./g;
my @jars;
if (@ARGV == 1) {
# expand classpath vars
@jars = split(":", shift);
} else {
@jars = @ARGV;
}
my (%found, $cmd, $found);
for my $jar (@jars) {
print "Searching $jar ...\n";
$cmd = "$cfg::unzip -l $jar";
my $pid = open(CMD, "-|");
unless ($pid) {
exec($cmd) || die "can't exec '$cmd': $!";
}
while (<CMD>) {
next unless /(.*?)([^\s\t]+\.class)/;
my $info = $1;
my $class = $2;
if ($class =~ /$pattern/) {
$found = 1;
$info =~ s/^[\s\t]+//o;
$info =~ s/[\s\t]+$//o;
$found{$jar}{$class} = $info;
}
}
close(CMD) || warn "failed to close CMD: $?";
}
unless ($found) { print "No classes found!\n"; exit; }
for my $jar (keys %found) {
print "Found in $jar";
if (exists $opts{s}) {
print " - " .
scalar keys(%{$found{$jar}}) . "\n";
next;
}
print ":\n";
for (sort keys %{$found{$jar}}) {
my $info = $found{$jar}{$_};
s/\.class$//o;
s/\//\./g;
print " " . $_ . " ($info)\n";
}
}
##---------------------------------------------
## SUBROUTINES
##---------------------------------------------
sub help
{
print qq~
jfind [-help] [-s] <pattern> <jars ...>
Searches for java classes matching a pattern
in a set of given jar files or classpath.
Class <pattern> could contain /. E.g.:
javax/ejb/EJBObject
the script will convert all / to .
EXAMPLES:
jfind digester *.jar
jfind -s digester *.jar
Search classpath...
jfind org.foo.Bar /usr/lib/foo.jar:/usr/lib/bar.jar
~;
exit;
}