---
title: "（转）精简JRE第二步 ─ 精简lib目录"
author: "yaojialing"
date: "2011-06-13T17:33:49Z"
tags: ["EXT", "Java", "Security", "正则表达式", "虚拟机"]
categories: ["编程语言"]
slug: "jre-lib"
draft: false
lastmod: "2011-06-13T17:34:37Z"
id: 1084397
url: "https://yaojialing.iteye.com/blog/1084397"
---

文章转自：[http://jarg.iteye.com/blog/867960](http://jarg.iteye.com/blog/867960)

 

 

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

 

 

 

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

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命令](/blog/866301) 中有初步介绍.

 

二、修改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代码 [![复制代码](http://jarg.iteye.com/images/icon_copy.gif)](http://jarg.iteye.com/blog/867960 "复制代码") [![收藏代码](http://jarg.iteye.com/images/icon_star.png)![](http://jarg.iteye.com/images/spinner.gif)]()

01. java/lang/Object
02. java/io/Serializable
03. java/lang/Comparable
04. ...
05. java/util/TreeMap$Entry
06. sun/misc/VM sun/nio/cs/ext/GBK
07. java/lang/StringCoding
08. java/lang/ThreadLocal$ThreadLocalMap
09. ...
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代码 [![复制代码](http://jarg.iteye.com/images/icon_copy.gif)](http://jarg.iteye.com/blog/867960 "复制代码") [![收藏代码](http://jarg.iteye.com/images/icon_star.png)![](http://jarg.iteye.com/images/spinner.gif)]()

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

```
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的工作.  
![](http://dl.iteye.com/upload/attachment/388458/82e02f4a-29ed-3b48-872c-89b48c1978ab.jpg)  
 

 

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

![](http://dl.iteye.com/upload/attachment/388462/ab5f5be8-6291-3db7-8909-cdb328f269d4.jpg)

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