在外国技术论坛看到一个使用Java API中的java.util.jar 类来创建和编译Jar的小程序。我参照那个小程序,编写了一个例子。创建一个
Test.java并放在在pack目录里,在文件所在目录编译Java源码,这时将建立一个包含目录中所有文件的所有JAR压缩文件。源码如下:
1 import java.io.*;
2 import java.util.*;
3 import java.util.jar.*;
4
5 public class Util {
6 private String dirName = "D:/3000/compileTest";
7 private String srcDirName = dirName + "/src";
8 private String jarDirName = dirName + "/jar";
9
10 public static void main(String[] args) {
11 Util u = new Util();
12 u.test();
13 }
14
15 public void test() {
16 this.writeJavaSource();
17 List srcList = new LinkedList();
18 this.addFilesFromDirectory(new File(this.srcDirName), srcList, ".java");
19 this.compileAll(srcList);
20 this.createJar(this.jarDirName + "/test.jar", new File(this.srcDirName));
21 }
22
23 public void writeJavaSource() {
24 File dir = new File(this.srcDirName + "/pack");
25 if (!dir.isDirectory()) {
26 dir.mkdir();
27 }
28 File file = new File(this.srcDirName + "/pack", "Test.java");
29 PrintWriter writer;
30 try {
31 writer = new PrintWriter(new FileWriter(file));
32 writer.println("package pack;");
33 writer.println();
34
35 writer.println("public class Test {");
36 writer.println(""tpublic void test() {");
37 writer.println(""t"tSystem.out.println(""test"");");
38 writer.println(""t}");
39 writer.println("}");
40 writer.close();
41 } catch (IOException e) {
42 e.printStackTrace();
43 }
44 }
45
46 public void compileAll(List allFileNames) {
47 Iterator it = allFileNames.iterator();
48 String[] allFiles = new String[allFileNames.size()];
49 int index = 0;
50 while (it.hasNext()) {
51 String currFilename = (String) it.next();
52 System.out.println("Adding " + currFilename + " to compilation");
53 allFiles[index++] = currFilename;
54 }
55 System.out.println("Starting compilation process on " + index
56 + " files");
57 long time0 = System.currentTimeMillis();
58 int compileReturnCode = com.sun.tools.javac.Main.compile(allFiles);
59 long time1 = System.currentTimeMillis();
60 System.out.println((time1 - time0) + " milliseconds, "
61 + ((compileReturnCode == 0) ? "success" : "failure"));
62 }
63 public void addFilesFromDirectory(File directory, List allFiles, String extension) {
64 File[] fileArray = directory.listFiles();
65 for (int i=0; i<fileArray.length; i++) {
66 File currFile = fileArray[i];
67 if (currFile.isFile()) {
68 String name = currFile.getAbsolutePath();
69 if (name.endsWith(extension))
70 allFiles.add(name);
71 }
72 else
73 addFilesFromDirectory(currFile, allFiles, extension);
74 }
75 }
76
77 public void addDirectoriesFromDirectory(File directory, List dirFiles) {
78 File[] fileArray = directory.listFiles();
79 for (int i=0; i<fileArray.length; i++) {
80 File currFile = fileArray[i];
81 if (!currFile.isFile()) {
82 String name = currFile.getAbsolutePath();
83 dirFiles.add(name);
84 addDirectoriesFromDirectory(currFile, dirFiles);
85 }
86 }
87 }
88
89 public void createJar(String jarName, File directory) {
90 Manifest manifest = new Manifest();
91 manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
92
93 File jarFile = new File(jarName);
94 try {
95 OutputStream fileOut = new FileOutputStream(jarFile);
96 JarOutputStream jarOut = new JarOutputStream(fileOut, manifest);
97
98 createJar(jarOut, directory);
99 jarOut.flush();
100 jarOut.finish();
101 }
102 catch (Exception e) {
103 e.printStackTrace();
104 }
105 System.out.println("Created " + jarName);
106 }
107
108 public void createJar(JarOutputStream jarOut, File directory) {
109 try {
110 File[] fileArray = directory.listFiles();
111 byte buffer[] = new byte[1024];
112 int bytesRead;
113 for (int i=0; i<fileArray.length; i++) {
114 File currFile = fileArray[i];
115 if (currFile.isDirectory()) {
116 createJar(jarOut, currFile);
117 }
118 else {
119 String currFilename = currFile.getName();
120 // 为了防止将完整文件名都放进JAR中必须分离文件名为目录和类名
121
122 String pathInJar = directory.getAbsolutePath();
123 pathInJar = pathInJar.substring(this.srcDirName.length()+1);
124
125 JarEntry entry = new JarEntry(pathInJar + "/" + currFilename);
126 jarOut.putNextEntry(entry);
127 entry.setMethod(JarEntry.DEFLATED);
128
129 File file = new File(directory, currFilename);
130 FileInputStream fis = new FileInputStream(file);
131 while ((bytesRead = fis.read(buffer)) != -1) {
132 jarOut.write(buffer, 0, bytesRead);
133 }
134 fis.close();
135 jarOut.flush();
136 jarOut.closeEntry();
137 }
138 }
139 } catch (IOException ex) {
140 ex.printStackTrace();
141 }
142 }
143 }
凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。