1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.Enumeration; 4 import java.util.List; 5 import java.util.zip.ZipEntry; 6 import java.util.zip.ZipFile; 7 8 public class FindInJar { 9 public String className; 10 11 public ArrayList jarFiles = new ArrayList(); 12 13 public FindInJar() { 14 } 15 16 public FindInJar(String className) { 17 this.className = className; 18 } 19 20 public void setClassName(String className) { 21 this.className = className; 22 } 23 24 public List findClass(String dir, boolean recurse) { 25 searchDir(dir, recurse); 26 return this.jarFiles; 27 } 28 29 protected void searchDir(String dir, boolean recurse) { 30 try { 31 File d = new File(dir); 32 if (!d.isDirectory()) { 33 return; 34 } 35 File[] files = d.listFiles(); 36 for (int i = 0; i < files.length; i++) { 37 if (recurse && files[i].isDirectory()) { 38 searchDir(files[i].getAbsolutePath(), true); 39 } else { 40 String filename = files[i].getAbsolutePath(); 41 if (filename.endsWith(".jar")||filename.endsWith(".zip")) { 42 ZipFile zip = new ZipFile(filename); 43 Enumeration entries = zip.entries(); 44 while (entries.hasMoreElements()) { 45 ZipEntry entry = (ZipEntry) entries.nextElement(); 46 String thisClassName = getClassName(entry); 47 if (thisClassName.equals(this.className) || thisClassName.equals(this.className + ".class")) { 48 this.jarFiles.add(filename); 49 } 50 } 51 } 52 } 53 } 54 } catch (Exception e) { 55 e.printStackTrace(); 56 } 57 } 58 59 public List getFilenames() { 60 return this.jarFiles; 61 } 62 63 protected String getClassName(ZipEntry entry) { 64 StringBuffer className = new StringBuffer(entry.getName().replace('/', '.')); 65 return className.toString(); 66 } 67 68 public static void main(String args[]) { 69 //要查找类的完整限定名 70 FindInJar findInJar = new FindInJar("com.water.util.PageSupport"); 71 //制定jar包所在的路径 72 List jarFiles = findInJar.findClass("E:\\lgp\\MyEclipse 10\\jypt\\WebRoot\\WEB-INF\\lib", true); 73 if (jarFiles.size() == 0) { 74 System.out.println("没有找到该类"); 75 } else { 76 for (int i = 0; i < jarFiles.size(); i++) { 77 System.out.println(jarFiles.get(i)); 78 } 79 } 80 } 81 }