#!/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; }

In reply to jfind - java class search by vladb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.