一些相关的资料,提供链接如下:
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写了一个,没有时间仔细改,但能完任务了。代码如下:
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- //我裁剪出来的rt大小为500多k。然后将rt1里面的目录和文件打包成rt.zip,改名为rt.jar,然后替换原来的rt.jar。具体的步骤可以参考上面提到的那两篇文章。
- public class ReduceRt {
- // 文件拷贝
- public static boolean copy(String file1, String file2,String file3) {
- try // must try and catch,otherwide will compile error
- {
- // instance the File as file_in and file_out
- java.io.File file_in = new java.io.File(file1);
- java.io.File file_out = new java.io.File(file2);
- if(!file_out.exists()){
- file_out.mkdirs();
- }
- FileInputStream in1 = new FileInputStream(file_in);
- FileOutputStream out1 = new FileOutputStream(file3);
- byte[] bytes = new byte[1024];
- int c;
- while ((c = in1.read(bytes)) != -1)
- out1.write(bytes, 0, c);
- in1.close();
- out1.close();
- return (true); // if success then return true
- }
- catch (Exception e) {
- System.out.println("Error!");
- return (false); // if fail then return false
- }
- }
- // 读取路径,copy
- public static int dealClass(String needfile) throws IOException {
- int sn = 0; // 成功个数
- String Loaded = "Loaded";
- File usedclass = new File(needfile);
- if (usedclass.canRead()) {
- String line = null;
- LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(usedclass), "GBK"));
- while ((line = reader.readLine()) != null) {
- line = line.trim();
- if (line.lastIndexOf("rt.jar") != -1) {
- int start = line.indexOf(Loaded);
- if (start != -1) {
- int end = line.lastIndexOf("from");
- if(end != -1){
- String str = line.substring(start+Loaded.length()+1, end-1);
- Pattern p = Pattern.compile("\\.");
- Matcher m = p.matcher(str);
- String after = m.replaceAll("/");
- String file1 = after+".class";
- int hg = file1.lastIndexOf("/");
- if(hg != -1){
- String file2 = "rt1/"+file1.substring(0, hg);
- copy("rt/"+file1,file2,"rt1/"+file1);
- }
- }
- }
- }
- }
- }
- return sn;
- }
- public static void main(String[] args) {
- String needfile = "class.txt";
- try {
- int sn = dealClass(needfile);
- System.out.print(sn);
- } catch (IOException e) {
- // TODO 自动生成 catch 块
- e.printStackTrace();
- }
- }
- }