在jar中查找类

java 代码
 
  1. import  java.util.*;  
  2. import  java.io.*;  
  3.   
  4. public   class  FindJar  
  5. {  
  6.     public   static   int  count =  0 ;  
  7.   
  8.     public  FindJar()  
  9.     {}  
  10.   
  11.     private   static   void  FindClassInLocalSystem(String path, String classname)  
  12.     {  
  13.         if  (path.charAt(path.length() -  1 ) != '\\')  
  14.         {  
  15.             path += '\\';  
  16.         }  
  17.         File file = new  File(path);  
  18.         if  (!file.exists())  
  19.         {  
  20.             System.out.println("Error: Path not Existed! Please Check it out!" );  
  21.             return ;  
  22.         }  
  23.         String[] filelist = file.list();  
  24.         for  ( int  i =  0 ; i < filelist.length; i++)  
  25.         {  
  26.             File temp = new  File(path + filelist[i]);  
  27.             if  ((temp.isDirectory() && !temp.isHidden() && temp.exists()))  
  28.             {  
  29.                 FindClassInLocalSystem(path + filelist[i], classname);  
  30.             }  
  31.             else   
  32.             {  
  33.                 if  (filelist[i].endsWith( "jar" ))  
  34.                 {  
  35.                     try   
  36.                     {  
  37.                         java.util.jar.JarFile jarfile = new  java.util.jar.JarFile(  
  38.                                 path + filelist[i]);  
  39.                         for  (Enumeration e = jarfile.entries(); e  
  40.                                 .hasMoreElements();)  
  41.                         {  
  42.                             String name = e.nextElement().toString();  
  43.                             if  (name.equals(classname))  
  44.                             {  
  45.                                 System.out.println("No."  + ++FindJar.count);  
  46.                                 System.out.println("Jar Package:"  + path  
  47.                                         + filelist[i]);  
  48.                                 System.out.println(name);  
  49.                             }  
  50.                         }  
  51.                     }  
  52.                     catch  (Exception eee)  
  53.                     {}  
  54.                 }  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     static   public   void  main(String[] args)  
  60.     {  
  61.         if  (args.length <  2 )  
  62.         {  
  63.             showHowToUsage();  
  64.             return ;  
  65.         }  
  66.         String absoluteclassname = args[1 ].replace( '.' , '/') +  ".class" ;  
  67.   
  68.         System.out.println("Find class ["  + args[ 1 ] +  "] in Path ["  + args[ 0 ]  
  69.                 + "] Results:" );  
  70.         FindClassInLocalSystem(args[0 ], absoluteclassname);  
  71.         if  (FindJar.count ==  0 )  
  72.         {  
  73.             System.out.println("Error:Can't Find Such Jar File!" );  
  74.         }  
  75.         System.out  
  76.                 .println("Find Process Ended! Total Results:"  + FindJar.count);  
  77.     }  
  78.   
  79.     public   static   void  showHowToUsage()  
  80.     {  
  81.         System.out  
  82.                 .println("Usage: Java -cp. JarClassFind <source path> <source class name>" );  
  83.         System.out  
  84.                 .println("Usage: Java -classpath. JarClassFind <source path> <source class name>" );  
  85.         System.out.println("" );  
  86.         System.out.println("<source path>:\t\tPath to Find eg:D:\\Jbuilder" );  
  87.         System.out  
  88.                 .println("<source class name>:\tClass to Find eg:java.applet.Applet" );  
  89.     }  
  90. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值