`
yaojialing
  • 浏览: 252709 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

(转)精简JRE第二步 ─ 精简lib目录

    博客分类:
  • JVM
阅读更多

文章转自:http://jarg.iteye.com/blog/867960

 

 

JRE(Java Runtime Environment): Java运行环境.

 

 

 


1. bin: 可以认为这是Java虚拟机.
精简过程在
本博客文章 精简JRE第一步 ─ 精简bin目录 中有初步介绍.


2. lib: 执行class文件时,Java虚拟机需要用到的类库及资源文件.

① lib目录最主要的类库是rt.jar,是任意Java程序所必需的类库.
lib目录大约62MB,但是rt.jar类库就占了47MB,可见精简bin目录,最主要是裁剪rt.jar.

 

② lib目录下一个运行Java程序不可或缺的文件是位于i386下的虚拟机配置文件jvm.cfg.该配置文件用来管理不同版本的jvm.dll.其内容作为java.exe,javac.exe的全局变量,用来加载相应的动态链接库文件.

 

③ lib目录里面除了包含程序运行所需要的类库及配置文件外,还包含有一些诸如: 鼠标光标,字体等系统资源.简单程序如果未用到这部分资源的话,可以剔除.
如果程序除去JRE部分,占用空间较大的话,为了避除资源加载错误带来的麻烦,建议保留这不到20MB的内容.

 

本文主要介绍类库的精简过程,主要是rt.jar类库的精简过程.

 

一、准备工作
查看执行当前Java程序所需加载的所有类(由于Java中具有类延时加载的现象,所以要把程序所有的功能都运行一次才能得到运行当前Java程序需要用到的全部类),并保存所有用到的类名到log.txt中.
查看程序加载类的方法在
本博客文章 精简JRE - verbose命令 中有初步介绍.

 

二、修改log.txt
由于log.txt每行都是形同: [Loaded java.lang.System from shared objects file]的一串字符,修改文本以方便获取类完整名java.lang.System,从而获得类似类路径java/lang/System的一串字符,方便后继编写类拷贝程序.
修改方法:
1. 查找并替换[Loaded 为空,达到删除[Loaded 的目的.
2. 使用任意一个具有正则表达式查找替换功能的文本编辑器,查找并替换 from.*为空,达到删除 from及其后面的字符串的目的.
3. 查找并替换.为/

4. 删除以[Opened 开头的行.

5. 删除程序中System.out.println的输出行.

Java代码 复制代码 收藏代码
  1. java/lang/Object    
  2. java/io/Serializable    
  3. java/lang/Comparable    
  4. ...    
  5. java/util/TreeMap$Entry    
  6. sun/misc/VM sun/nio/cs/ext/GBK    
  7. java/lang/StringCoding    
  8. java/lang/ThreadLocal$ThreadLocalMap    
  9. ...    
  10. java/security/ProtectionDomain$Key    
  11. java/security/Principal java/lang/Shutdown    
  12. java/lang/Shutdown$Lock  
java/lang/Object 
java/io/Serializable 
java/lang/Comparable 
... 
java/util/TreeMap$Entry 
sun/misc/VM sun/nio/cs/ext/GBK 
java/lang/StringCoding 
java/lang/ThreadLocal$ThreadLocalMap 
... 
java/security/ProtectionDomain$Key 
java/security/Principal java/lang/Shutdown 
java/lang/Shutdown$Lock

 

 

三、类拷贝程序
上述形同java/lang/System的字符串,System是类名,相应System.class文件.java/lang为System.class在类库rt.jar中的相对路径.下面的类拷贝程序的目的就是从解压后的rt.jar的文件夹rt中将需要用到的类(也就是log.txt中记载的类)拷贝到别一个文件夹(这里为rt1)中,达到抽取运行当前Java程序需要全部类的目的.

Java代码 复制代码 收藏代码
  1. import java.io.*;   
  2.   
  3. public class CopyClass   
  4. {   
  5.     public String src = "rt";       // 类源目录   
  6.     public String dest = "rt1";     // 类拷贝目的目录   
  7.   
  8.     public CopyClass()   
  9.     {   
  10.         readAndCopy("log.txt");   
  11.     }   
  12.   
  13.     public static void main(String[] args)   
  14.     {   
  15.         CopyClass obj = new CopyClass();   
  16.     }   
  17.   
  18.     /* 读取log.txt中内容,并拷贝相应类 */  
  19.     public void readAndCopy(String logName)   
  20.     {   
  21.         int count = 0;  // 用于记录成功拷贝的类数   
  22.         try  
  23.         {   
  24.             FileInputStream fi = new FileInputStream(logName);   
  25.             InputStreamReader ir = new InputStreamReader(fi);   
  26.             BufferedReader br = new BufferedReader(ir);   
  27.   
  28.             String string = br.readLine();   
  29.             while(string != null)   
  30.             {   
  31.                 if(copyClass(string) == true)   
  32.                     count++;   
  33.                 else  
  34.                     System.out.println("ERROR " + count + ": " + string);   
  35.                 string = br.readLine();   
  36.             }   
  37.         }   
  38.         catch (IOException e)   
  39.         {   
  40.             System.out.println("ERROR: " + e);   
  41.         }   
  42.         System.out.println("count: " + count);   
  43.     }   
  44.   
  45.     /* 从rt中拷贝出class文件 */  
  46.     public boolean copyClass(String string) throws IOException   
  47.     {   
  48.         String classDir = string.substring(0,string.lastIndexOf("/"));   
  49.         String className = string.substring(string.lastIndexOf("/")+1,string.length()) + ".class";   
  50.            
  51.         /* class文件不存在,返回false */  
  52.         File srcFile = new File(src + "/" + classDir + "/" + className);   
  53.         if(!srcFile.exists())   
  54.         {   
  55.             return false;   
  56.         }   
  57.         byte buf[] = new byte[256];   
  58.         FileInputStream fin = new FileInputStream(srcFile);   
  59.   
  60.         /* class目录不存在,创建 */  
  61.         File destDir = new File(dest + "/" + classDir);   
  62.         if(!destDir.exists())   
  63.             destDir.mkdirs();   
  64.   
  65.         File destFile = new File(destDir + "/" + className);   
  66.         FileOutputStream fout = new FileOutputStream(destFile);   
  67.         int len = 0;   
  68.         while((len = fin.read(buf)) != -1)   
  69.         {   
  70.             fout.write(buf,0,len);   
  71.         }   
  72.         fout.flush();   
  73.         return true;   
  74.     }   
  75. }  
import java.io.*;

public class CopyClass
{
	public String src = "rt";		// 类源目录
	public String dest = "rt1";		// 类拷贝目的目录

	public CopyClass()
	{
		readAndCopy("log.txt");
	}

	public static void main(String[] args)
	{
		CopyClass obj = new CopyClass();
	}

	/* 读取log.txt中内容,并拷贝相应类 */
	public void readAndCopy(String logName)
	{
		int count = 0;	// 用于记录成功拷贝的类数
		try
		{
			FileInputStream fi = new FileInputStream(logName);
			InputStreamReader ir = new InputStreamReader(fi);
			BufferedReader br = new BufferedReader(ir);

			String string = br.readLine();
			while(string != null)
			{
				if(copyClass(string) == true)
					count++;
				else
					System.out.println("ERROR " + count + ": " + string);
				string = br.readLine();
			}
		}
		catch (IOException e)
		{
			System.out.println("ERROR: " + e);
		}
		System.out.println("count: " + count);
	}

	/* 从rt中拷贝出class文件 */
	public boolean copyClass(String string) throws IOException
	{
		String classDir = string.substring(0,string.lastIndexOf("/"));
		String className = string.substring(string.lastIndexOf("/")+1,string.length()) + ".class";
		
		/* class文件不存在,返回false */
		File srcFile = new File(src + "/" + classDir + "/" + className);
		if(!srcFile.exists())
		{
			return false;
		}
		byte buf[] = new byte[256];
		FileInputStream fin = new FileInputStream(srcFile);

		/* class目录不存在,创建 */
		File destDir = new File(dest + "/" + classDir);
		if(!destDir.exists())
			destDir.mkdirs();

		File destFile = new File(destDir + "/" + className);
		FileOutputStream fout = new FileOutputStream(destFile);
		int len = 0;
		while((len = fin.read(buf)) != -1)
		{
			fout.write(buf,0,len);
		}
		fout.flush();
		return true;
	}
}

 

 

程序运行要求: 事先解压rt.jar类库到该类拷贝程序所在目录

 

四、精简rt.jar
将拷贝出来的类打包成rt.jar.
1. 用jar打包命令打包.
2. 用压缩程序(如: winrar)压缩成rt.zip(不能是.rar格式),然后重命令为rt.jar.

 

五、精简charsets.jar
charsets.jar包含执行程序所需的编码方式,但是通常我们只会用到其中的一种.根据类拷贝程序提示的错误信息,将相应的类拷贝出来打包成charsets.jar,完成精简charsets.jar的工作.

 

 

至此完成本文要介绍的内容: 精简lib目录.




打开JRE安装目录.目录包括bin,lib二个文件夹.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics