<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
           最近因?yàn)楣ぷ餍枰淳幾goracle的jdbc driver,用了frontend plus,發(fā)現(xiàn)無(wú)法編譯整個(gè)jar,用起來(lái)比較繁瑣。于是,狠狠心寫(xiě)了這個(gè)么個(gè)工具類(lèi),時(shí)間有限,沒(méi)有什么設(shè)計(jì)思想,本著拿來(lái)就用的態(tài)度,很簡(jiǎn)陋的完成了這個(gè)小任務(wù)。 :) 基本思想是:能夠編譯指定目錄下的所有jar文件,并將文件生成到指定的目錄下,同時(shí)將所有的jad文件重名成java。


            這個(gè)工具依賴(lài)于jad, 所以需要預(yù)先安裝。執(zhí)行后會(huì)在outputDir下生成兩個(gè)目錄: unzip, decompile。用法如下:JarDecompiler <jadPath> [targetDir] [outputDir], 如果需要自定義的一些功能,大家可以根據(jù)Jad的一些option進(jìn)行修改。因?yàn)榛趓untime exec,我自己使用過(guò)程中發(fā)現(xiàn),如過(guò)jar過(guò)多、過(guò)大的話(huà),運(yùn)行一邊以后,發(fā)現(xiàn)并不是所有的文件都能反編譯出來(lái),可能需要連續(xù)執(zhí)行幾次才能將所有jar包編譯完成。這個(gè)問(wèn)題比較怪異,誰(shuí)有興趣的話(huà),可以看看。中間還有生成bat文件的代碼,不過(guò)代碼太多的話(huà),顯得比較亂,被我去掉了,有興趣的可以留下郵箱。


      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) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): FJin Dev

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

    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产精品高清久久久| 老汉色老汉首页a亚洲| 久久久久亚洲av无码专区喷水| 亚洲宅男精品一区在线观看| 男女猛烈激情xx00免费视频| 最近最好最新2019中文字幕免费 | 国产精品免费大片| 在线成人a毛片免费播放 | 亚洲综合色7777情网站777| 一本大道一卡二大卡三卡免费| 亚洲无砖砖区免费| 免费一级做a爰片久久毛片潮喷| 亚洲精品免费视频| 污视频网站在线观看免费| 亚洲国产精品免费在线观看| 亚洲中文无韩国r级电影| 成人亚洲国产va天堂| 精品国产免费一区二区三区香蕉| 在线观看亚洲免费视频| 91亚洲va在线天线va天堂va国产| 国产精品亚洲综合网站| 91九色视频无限观看免费| 伊人久久综在合线亚洲91| 亚洲欧美日韩一区二区三区在线| 免费人成黄页在线观看日本| 亚洲AV中文无码乱人伦在线视色 | 一个人看的www免费高清| 四虎国产精品免费久久| 久久亚洲精品成人| 三级片免费观看久久| 成年女人色毛片免费看| 久久精品a亚洲国产v高清不卡 | 国产成人涩涩涩视频在线观看免费 | 国产成人免费片在线观看| 亚洲国产精品久久丫| 国产午夜无码精品免费看| 亚洲国产精品狼友中文久久久| 亚洲一区二区三区丝袜| 3344永久在线观看视频免费首页| 亚洲精品成人片在线播放| 四虎影视久久久免费|