精简jre

一些相关的资料,提供链接如下:
java程序发布之jre篇 
基本知道思路了,把程序打包成jar,能双击运行了,然后拷贝一个jre到程序目录下,具体是这样的,目录叫dict,dict下面有dict.jar、jre(目录),然后写了一个cmd脚本:
@echo off
set path=%cd%\jre\bin
java -jar -verbose:class dict.jar >>class.txt
pause
 
这样程序使用的就是当前目录下的jre,程序运行后,最好把所有的功能使用一遍,这样输出了一个文件class.txt,里面有所有需要的class,其格式如下:
[Opened D:\data\dict\jre\lib\rt.jar]
[Loaded java.lang.Object from D:\data\dict\jre\lib\rt.jar]
[Loaded java.io.Serializable from D:\data\dict\jre\lib\rt.jar]
[Loaded java.lang.Comparable from D:\data\dict\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from D:\data\dict\jre\lib\rt.jar]
[Loaded org.apache.lucene.index.CompoundFileReader$FileEntry from file:/D:/data/dict/dict.jar]
 

因为只涉及到 rt.jar ,按一下方式裁剪rt.jar(如果还涉及到其他jar,则按照以下方式裁剪他们):
首先在utralEdit中进行一些处理,去掉所有不是rt.jar中的class的行,去掉from后面的,去掉loaded等无关项目,再把“.”替换成“/”.这个可以利用正则表达式等轻松处理。处理完后得到的文件类似如下格式:
java/lang/Object
java/io/Serializable
java/lang/Comparable
java/lang/CharSequence
java/lang/String

然后写一个脚本或者程序处理,将rt中需要的的class拷贝到另一个对应的文件夹rt1,我用java写了一个,没有时间仔细改,但能完任务了。代码如下:

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.LineNumberReader;  
  7. import java.util.regex.Matcher;  
  8. import java.util.regex.Pattern;  
  9.   
  10. //我裁剪出来的rt大小为500多k。然后将rt1里面的目录和文件打包成rt.zip,改名为rt.jar,然后替换原来的rt.jar。具体的步骤可以参考上面提到的那两篇文章。  
  11.   
  12. public class ReduceRt {  
  13.     // 文件拷贝  
  14.     public static boolean copy(String file1, String file2,String file3) {  
  15.         try // must try and catch,otherwide will compile error  
  16.         {  
  17.             // instance the File as file_in and file_out  
  18.             java.io.File file_in = new java.io.File(file1);  
  19.             java.io.File file_out = new java.io.File(file2);  
  20.             if(!file_out.exists()){  
  21.                 file_out.mkdirs();  
  22.             }  
  23.             FileInputStream in1 = new FileInputStream(file_in);  
  24.             FileOutputStream out1 = new FileOutputStream(file3);  
  25.             byte[] bytes = new byte[1024];  
  26.             int c;  
  27.             while ((c = in1.read(bytes)) != -1)  
  28.                 out1.write(bytes, 0, c);  
  29.             in1.close();  
  30.             out1.close();  
  31.             return (true); // if success then return true  
  32.         }  
  33.   
  34.         catch (Exception e) {  
  35.             System.out.println("Error!");  
  36.             return (false); // if fail then return false  
  37.         }  
  38.     }  
  39.   
  40.     // 读取路径,copy  
  41.     public static int dealClass(String needfile) throws IOException {  
  42.         int sn = 0// 成功个数  
  43.         String Loaded = "Loaded";  
  44.   
  45.         File usedclass = new File(needfile);  
  46.         if (usedclass.canRead()) {  
  47.             String line = null;  
  48.             LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(usedclass), "GBK"));  
  49.             while ((line = reader.readLine()) != null) {  
  50.                 line = line.trim();  
  51.                 if (line.lastIndexOf("rt.jar") != -1) {  
  52.                     int start = line.indexOf(Loaded);  
  53.                     if (start != -1) {  
  54.                         int end = line.lastIndexOf("from");  
  55.                         if(end != -1){  
  56.                             String str = line.substring(start+Loaded.length()+1, end-1);  
  57.                             Pattern p = Pattern.compile("\\.");  
  58.                             Matcher m = p.matcher(str);  
  59.                             String after = m.replaceAll("/");  
  60.                             String file1 = after+".class";  
  61.                             int hg = file1.lastIndexOf("/");  
  62.                               
  63.                             if(hg != -1){  
  64.                                 String file2 = "rt1/"+file1.substring(0, hg);  
  65.                                 copy("rt/"+file1,file2,"rt1/"+file1);  
  66.                             }  
  67.                         }  
  68.                           
  69.                     }  
  70.                 }  
  71.   
  72.             }  
  73.         }  
  74.         return sn;  
  75.   
  76.     }  
  77.   
  78.     public static void main(String[] args) {  
  79.         String needfile = "class.txt";  
  80.         try {  
  81.             int sn = dealClass(needfile);  
  82.             System.out.print(sn);  
  83.         } catch (IOException e) {  
  84.             // TODO 自动生成 catch 块  
  85.             e.printStackTrace();  
  86.         }  
  87.   
  88.     }  
  89.   
  90. }  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值