Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

jfind - java class search

by vladb (Vicar)
on Feb 10, 2006 at 21:15 UTC ( #529473=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Vladimir Bogdanov (contact at vladb.com)
Description:
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;
}

Comment on jfind - java class search
Download Code
Re: jfind - java class search
by newroot (Initiate) on Mar 24, 2010 at 07:19 UTC
    Perhaps you can try classsearch for java.
    It's a GUI based tool and it will help decompile the class. It's powerful.
    classsearch

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://529473]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2014-12-26 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls