I want to know the given class name exists in which jar file?

I want to know the given class name exists in which jar file? C:\Program Files\IBM\IBMIMShared\plugins in this path around 600 jar files are there. i want to know the name of the jar file where 'com.rational.ft.util.debug' class exists.

View Answers

February 17, 2011 at 5:57 PM

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");
   }
}









Related Tutorials/Questions & Answers:
Advertisements