Java find the jar of the given class

import java.io.*;
import java.util.*;
import java.util.jar.*;
class OnlyExt implements FilenameFilter{
  String ext;
  public OnlyExt(String ext){
  this.ext="." + ext;
  }
  public boolean accept(File dir,String name){
  return name.endsWith(ext);
  }
}
public class FindJar{

 private static boolean debug = true;

 public static void getClasseNamesInPackage(String packageName){
   ArrayList classes = new ArrayList();

   packageName = packageName.replaceAll("\\." , "/");
   if (debug) 
   try{
       FilenameFilter ff = new OnlyExt("jar");
        File folder = new File 
                   ("C:/Program Files/Java/jdk1.6.0_20/lib/");
        File[] files = folder.listFiles(ff);
        for(int i=0;i<files.length;i++){
     JarInputStream jarFile = new JarInputStream(new FileInputStream  
(files[i].getPath()));
     JarEntry jarEntry;

     while(true) {
       jarEntry=jarFile.getNextJarEntry ();
       if(jarEntry == null){
         break;
       }
       if((jarEntry.getName ().startsWith (packageName)) &&
            (jarEntry.getName ().endsWith (".class")) ) {
         if (debug) System.out.println   
("Found " + jarEntry.getName().replaceAll("/", "\\.")+" in "+files[i].getName());
         classes.add (jarEntry.getName().replaceAll("/", "\\."));
       }
     }
        }
   }
   catch( Exception e){
     e.printStackTrace ();
   }
}
  public static void main (String[] args){
   FindJar.getClasseNamesInPackage("org.jfree.chart.ChartFactory");
   }
}

Comments