<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 42,comments - 83,trackbacks - 0
           最近因為工作需要反編譯oracle的jdbc driver,用了frontend plus,發現無法編譯整個jar,用起來比較繁瑣。于是,狠狠心寫了這個么個工具類,時間有限,沒有什么設計思想,本著拿來就用的態度,很簡陋的完成了這個小任務。 :) 基本思想是:能夠編譯指定目錄下的所有jar文件,并將文件生成到指定的目錄下,同時將所有的jad文件重名成java。


            這個工具依賴于jad, 所以需要預先安裝。執行后會在outputDir下生成兩個目錄: unzip, decompile。用法如下:JarDecompiler <jadPath> [targetDir] [outputDir], 如果需要自定義的一些功能,大家可以根據Jad的一些option進行修改。因為基于runtime exec,我自己使用過程中發現,如過jar過多、過大的話,運行一邊以后,發現并不是所有的文件都能反編譯出來,可能需要連續執行幾次才能將所有jar包編譯完成。這個問題比較怪異,誰有興趣的話,可以看看。中間還有生成bat文件的代碼,不過代碼太多的話,顯得比較亂,被我去掉了,有興趣的可以留下郵箱。


      1 package test.decompiler;
      2 
      3 import java.io.File;
      4 import java.io.IOException;
      5 import java.util.jar.JarFile;
      6 import java.util.jar.JarEntry;
      7 import java.io.InputStream;
      8 import java.io.FileOutputStream;
      9 import java.io.DataOutputStream;
     10 import java.util.ArrayList;
     11 
     12 
     13 public class JarDecompiler {
     14     
     15     private String jadPath = null;
     16     private String targetDir = ".";
     17     private String outputDir = ".";
     18     private File unzipFile = null
     19     private static String UNZIP = "unzip";
     20     private static String DECOMPILE="decompile";
     21     
     22     private ArrayList jarFileList = null;
     23     private ArrayList dirList = null;
     24     private ArrayList fileList = null;
     25     
     26 
     27     public JarDecompiler(String jadPath, String targetDir, String outputDir) {
     28         // TODO Auto-generated constructor stub
     29         this.jadPath = jadPath;
     30         if(targetDir != null)
     31             this.targetDir = targetDir;
     32         if(outputDir != null)
     33             this.outputDir = outputDir;
     34         this.outputDir = this.outputDir + System.getProperty("file.separator");
     35         jarFileList = new ArrayList();
     36         dirList = new ArrayList();
     37         fileList = new ArrayList();
     38     }
     39 
     40     /**
     41      * @param args
     42      */
     43     public static void main(String[] args) {
     44         // TODO Auto-generated method stub
     45         JarDecompiler decompiler = null;
     46         if((args.length < 1|| (args.length > 3))
     47         {
     48             usage();
     49             System.exit(1);
     50         }
     51         else if(args.length ==1)
     52             decompiler = new JarDecompiler(args[0], nullnull);
     53         else if(args.length ==2)
     54             decompiler = new JarDecompiler(args[0], args[1], null);
     55         else if(args.length ==3)
     56             decompiler = new JarDecompiler(args[0], args[1], args[2]);
     57         decompiler.run();
     58     }
     59     
     60     public void run()
     61     {
     62         if(initializeFiles())
     63         {
     64             getJarFileNameList(targetDir);
     65             //printArrayList(jarFileList);
     66             for(int loop=0; loop<jarFileList.size(); loop++)
     67             {
     68                 unzipJar((String)jarFileList.get(loop));                
     69             }
     70             getDirNameList(outputDir + UNZIP);
     71             //printArrayList(dirList);
     72             checkDir(outputDir + DECOMPILE);
     73             String dirName= null;
     74             for(int i=0; i<dirList.size(); i++)
     75             {
     76                 dirName = (String)dirList.get(i);
     77                 dirName = dirName.replaceFirst(UNZIP, DECOMPILE);
     78                 checkDir(dirName);
     79             }
     80             decompile();
     81             getFileNameList(outputDir + DECOMPILE);
     82             //printArrayList(fileList);
     83             renameFiles();
     84             //printJarFiles();
     85             unzipFile.delete();
     86         }
     87     }
     88     
     89     private void printArrayList(ArrayList list)
     90     {
     91         for(int loop=0; loop<list.size(); loop++)
     92         {
     93             System.out.println(list.get(loop));
     94         }        
     95     }
     96     
     97     private boolean initializeFiles()
     98     {
     99         boolean ret = true;
    100         unzipFile = new File(outputDir + UNZIP);
    101         
    102         if(unzipFile.exists())
    103         {
    104             //ret = false;
    105             System.out.println(UNZIP + " has been existed in " + outputDir);
    106         }else
    107         {
    108             try
    109             {
    110                 unzipFile.mkdir();
    111             }catch(Exception e)
    112             {
    113                 e.printStackTrace();
    114             }
    115         }    
    116         return ret;
    117     }
    118     
    119     private void getJarFileNameList(String targetDir)
    120     {
    121         File[] files = null;
    122         File target = new File(targetDir);
    123         if(target.exists() && target.isDirectory())
    124         {
    125             files = target.listFiles();
    126             for(int loop=0; loop<files.length; loop++)
    127             {
    128                 if(files[loop].isFile() && files[loop].getName().endsWith(".jar"))
    129                 {
    130                     jarFileList.add(files[loop].getAbsolutePath());
    131                 }else if(files[loop].isDirectory())
    132                 {
    133                     getJarFileNameList(files[loop].getAbsolutePath());
    134                 }
    135             }
    136         }
    137     }
    138     
    139     private void getDirNameList(String dirName)
    140     {
    141         File[] files = null;
    142         File target = new File(dirName);
    143         if(target.exists() && target.isDirectory())
    144         {
    145             files = target.listFiles();
    146             for(int loop=0; loop<files.length; loop++)
    147             {
    148                 if(files[loop].isDirectory())
    149                 {
    150                     dirList.add(files[loop].getAbsolutePath());
    151                     getDirNameList(files[loop].getAbsolutePath());
    152                 }
    153             }
    154         }
    155     }
    156     
    157     private void getFileNameList(String dirName)
    158     {
    159         File[] files = null;
    160         File target = new File(dirName);
    161         if(target.exists() && target.isDirectory())
    162         {
    163             files = target.listFiles();
    164             for(int loop=0; loop<files.length; loop++)
    165             {
    166                 if(files[loop].isDirectory())
    167                 {
    168                     getFileNameList(files[loop].getAbsolutePath());
    169                 }else if(files[loop].isFile())
    170                 {
    171                     fileList.add(files[loop].getAbsolutePath());    
    172                 }
    173             }
    174         }
    175     }
    176     
    177     private  boolean unzipJar(String fileName)
    178     {
    179         boolean ret = true;
    180         String postfixName = fileName.substring(outputDir.length());
    181         String zipTo = outputDir + UNZIP + System.getProperty("file.separator");
    182         
    183         Object[] dirs = getDirs(postfixName).toArray();
    184         
    185         checkDir(zipTo);
    186         for(int loop=0; loop<dirs.length; loop++)
    187         {
    188             zipTo = zipTo + (String)dirs[loop];
    189             if(zipTo.endsWith(".jar"))
    190             {
    191                 zipTo.substring(0, zipTo.length() -4);
    192             }
    193             zipTo = zipTo + System.getProperty("file.separator");
    194             checkDir(zipTo);
    195         }
    196         
    197         try
    198         {
    199             String cmd = "unzip -d " + zipTo + " " + fileName;
    200             Runtime.getRuntime().exec(cmd);
    201         }catch(IOException ioe)
    202         {
    203             ioe.printStackTrace();
    204         }
    205         
    206         return ret;
    207     }
    208     
    209     public void decompile()
    210     {
    211         
    212         String target = null;
    213         String output = null;
    214         String decopCmd = null;
    215         InputStream es = null;
    216 
    217         Process decompP = null;
    218         int result = -1;
    219         byte[] toRead = new byte[2048];
    220         for(int i=0; i<dirList.size(); i++)
    221         {
    222             target = (String)dirList.get(i);
    223             output = target;
    224             output = output.replaceFirst(UNZIP, DECOMPILE);
    225             decopCmd = "jad -ff -d " + output + " " + target + 
    226                 System.getProperty("file.separator"+ "*.class";
    227             decopCmd = jadPath + System.getProperty("file.separator"+ decopCmd;
    228             
    229             //System.out.println(decopCmd);
    230             try
    231             {
    232                 decompP = Runtime.getRuntime().exec(decopCmd);
    233                 es = decompP.getErrorStream();
    234                 /*
    235                 while(es.read(toRead) > 0)
    236                 {
    237                     System.out.println(new String(toRead));
    238                 }
    239                 */
    240             }catch(Exception e)
    241             {
    242                 e.printStackTrace();
    243             }
    244         }
    245     }
    246     
    247     
    248     private void renameFiles()
    249     {
    250         String target = null;
    251         String output = null;
    252         String renameCmd = null;
    253         File targetFile = null;
    254         File destFile = null;
    255         Process p = null;
    256         
    257         for(int i=0; i<fileList.size(); i++)
    258         {
    259             if(((String)fileList.get(i)).endsWith(".jad"))
    260             {
    261                 target = (String)fileList.get(i);
    262                 output = target;
    263                 output = output.replaceFirst(UNZIP, DECOMPILE);
    264                 
    265                 targetFile = new File(output);
    266                 //System.out.println("rename from: " + output + " to: " + output.substring(0, output.length() -4) + ".java");
    267                 destFile = new File(output.substring(0, output.length() -4+ ".java");
    268                 targetFile.renameTo(destFile);
    269                 targetFile.delete();
    270                 try
    271                 {
    272                     Thread.currentThread().sleep(100);
    273                 }catch(Exception e)
    274                 {
    275                     e.printStackTrace();
    276                 }
    277                 
    278                 /*
    279                 renameCmd = "cmd /c ren " + output + " " + output.substring(0, output.length() -4) + ".java";
    280                 System.out.println(renameCmd);
    281                 try
    282                 {
    283                     p = Runtime.getRuntime().exec(renameCmd);
    284                     p.waitFor();
    285                 }catch(Exception e)
    286                 {
    287                     e.printStackTrace();
    288                 }
    289                 */
    290                 
    291             }
    292             targetFile = null;
    293             destFile = null;
    294         }
    295     }
    296     
    297     private void checkDir(String name)
    298     {
    299         File file = new File(name);
    300         if(!file.exists())
    301         {
    302             file.mkdir();
    303         }
    304     }
    305     
    306     public ArrayList getDirs(String name)
    307     {
    308         ArrayList list = new ArrayList();
    309         while(name.indexOf(System.getProperty("file.separator")) != -1)
    310         {
    311             list.add(name.substring(0, name.indexOf(System.getProperty("file.separator"))));
    312             name = name.substring(name.indexOf( System.getProperty("file.separator"))+1);
    313         }
    314         
    315         list.add(name.substring(0, name.length()-4));
    316         return list;
    317     }
    318     
    319     private static void usage()
    320     {
    321         System.out.println("usage: java test.decompiler.JarDecompiler <Jad Path> [target Dir] [output Dir]");
    322     }
    323 
    324 }
    325 


    posted on 2008-09-11 18:26 走走停停又三年 閱讀(2215) 評論(1)  編輯  收藏 所屬分類: FJin Dev

    FeedBack:
    # re: 基于Jad的批量反編譯工具JarDecompiler
    2009-09-16 08:21 | sorcerer
    試了一下,好像都要執行2次才能反編譯,一個200多k的jar也是這樣,第一次建了unzip目錄和class,還有decompile下的文件夾,但是沒有反編譯出類文件.  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲情A成黄在线观看动漫软件| 操美女视频免费网站| 亚洲日本va午夜中文字幕一区| 在线综合亚洲欧洲综合网站| 精品久久洲久久久久护士免费| 91在线精品亚洲一区二区| 免费人成在线视频| 菠萝菠萝蜜在线免费视频| 亚洲电影免费在线观看| 成年人性生活免费视频| 久久www免费人成看国产片| 久久久无码精品亚洲日韩按摩| 日韩a级无码免费视频| 色偷偷女男人的天堂亚洲网| 国产免费看插插插视频| 久久久久久久99精品免费观看| 国产午夜亚洲精品午夜鲁丝片| 免费一级特黄特色大片| 亚洲av无码一区二区三区乱子伦| 久久精品电影免费动漫| 国产精品亚洲AV三区| 亚洲人成人77777网站| 成年美女黄网站色大免费视频| 亚洲综合激情五月色一区| 国产精品亚洲片在线观看不卡 | 亚洲成a人片77777群色| 全部免费国产潢色一级| 无码中文字幕av免费放dvd| 羞羞漫画小舞被黄漫免费| 亚洲无人区视频大全| 亚洲综合AV在线在线播放| 白白国产永久免费视频| 巨波霸乳在线永久免费视频 | 亚洲av色香蕉一区二区三区 | 中文字幕无码日韩专区免费| 毛片亚洲AV无码精品国产午夜| 无码国模国产在线观看免费| 99久久精品免费视频| 中文字幕乱码免费看电影| 国产亚洲精品成人久久网站| 亚洲毛片基地日韩毛片基地|