`

查找某个类所在jar包

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

相关推荐

Global site tag (gtag.js) - Google Analytics