查看Class从那个jar文件加载及在jar文件中查找Class

查看类是从哪个Jar包中加载的:
java 代码
 
  1. ProtectionDomain pd = MyCase.class.getProtectionDomain();  
  2. CodeSource cs = pd.getCodeSource();  
  3. System.out.println(cs.getLocation());  


查找类在某个目录下哪个Jar包中:

java 代码
 
  1. package 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 FindJarOfClass {  
  11.     public String className;  
  12.   
  13.     public ArrayList jarFiles = new ArrayList();  
  14.   
  15.     public FindJarOfClass() {  
  16.     }  
  17.   
  18.     public FindJarOfClass(String className) {  
  19.         this.className = className;  
  20.     }  
  21.   
  22.     public void setClassName(String className) {  
  23.         this.className = className;  
  24.     }  
  25.   
  26.     public List findClass(String dir, boolean recurse) {  
  27.         searchDir(dir, recurse);  
  28.         return this.jarFiles;  
  29.     }  
  30.   
  31.     protected void searchDir(String dir, boolean recurse) {  
  32.         try {  
  33.             File d = new File(dir);  
  34.             if (!d.isDirectory()) {  
  35.                 return;  
  36.             }  
  37.             File[] files = d.listFiles();  
  38.             for (int i = 0; i < files.length; i++) {  
  39.                 if (recurse && files[i].isDirectory()) {  
  40.                     searchDir(files[i].getAbsolutePath(), true);  
  41.                 } else {  
  42.                     String filename = files[i].getAbsolutePath();  
  43.                     if (filename.endsWith(".jar")||filename.endsWith(".zip")) {  
  44.                         ZipFile zip = new ZipFile(filename);  
  45.                         Enumeration entries = zip.entries();  
  46.                         while (entries.hasMoreElements()) {  
  47.                             ZipEntry entry = (ZipEntry) entries.nextElement();  
  48.                             String thisClassName = getClassName(entry);  
  49.                             if (thisClassName.equals(this.className) || thisClassName.equals(this.className + ".class")) {  
  50.                                 this.jarFiles.add(filename);  
  51.                             }  
  52.                         }  
  53.                     }  
  54.                 }  
  55.             }  
  56.         } catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60.   
  61.     public List getFilenames() {  
  62.         return this.jarFiles;  
  63.     }  
  64.   
  65.     protected String getClassName(ZipEntry entry) {  
  66.         StringBuffer className = new StringBuffer(entry.getName().replace('/', '.'));  
  67.         return className.toString();  
  68.     }  
  69.   
  70.     public static void main(String args[]) {  
  71.         FindJarOfClass findInJar = new FindJarOfClass("org.apache.tuscany.test.SCATestCase");  
  72.         List jarFiles = findInJar.findClass("E:/yanhua/library/tuscany-jar"true);  
  73.         if (jarFiles.size() == 0) {  
  74.             System.out.println("Not Found");  
  75.         } else {  
  76.             for (int i = 0; i < jarFiles.size(); i++) {  
  77.                 System.out.println(jarFiles.get(i));  
  78.             }  
  79.         }  
  80.     }  
  81. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值