1 package auxtestlib;
2
3 import java.io.File;
4 import java.io.IOException;
5
6
7
8
9 public final class JarUtils {
10
11
12
13 private JarUtils() {
14
15
16
17 }
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public static File makeJar(File directory, File jarFile) throws IOException {
33 if (directory == null) {
34 throw new IllegalArgumentException("directory == null");
35 }
36
37 if (!directory.exists()) {
38 throw new IllegalArgumentException("Directory '"
39 + directory.getAbsolutePath() + "' doesnt't exist.");
40 }
41
42 if (!directory.isDirectory()) {
43 throw new IllegalArgumentException("File '"
44 + directory.getAbsolutePath() + "' is not a directory.");
45 }
46
47 File parent = directory.getParentFile();
48 if (parent == null) {
49 throw new IllegalArgumentException("Directory '"
50 + directory.getAbsolutePath() + "' does not name a parent.");
51 }
52
53 File jfile = jarFile;
54
55 if (jfile == null) {
56 jfile = new File(parent, directory.getName() + ".jar");
57 }
58
59 if (jfile.exists()) {
60 throw new IllegalArgumentException("Jar file '"
61 + jfile.getAbsolutePath() + "' already exists.");
62 }
63
64 String args[] = new String[] { "jar", "cf", jfile.getAbsolutePath(),
65 directory.getName() };
66
67 CommandRunner cr = new CommandRunner();
68
69
70 cr.run_command(args, parent, 60);
71 return jfile;
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public static File makeFullJar(File directory, File jarFile)
88 throws IOException {
89 if (directory == null) {
90 throw new IllegalArgumentException("directory == null");
91 }
92
93 if (jarFile == null) {
94 throw new IllegalArgumentException("jarFile == null");
95 }
96
97 if (!directory.exists()) {
98 throw new IllegalArgumentException("Directory '"
99 + directory.getAbsolutePath() + "' doesnt't exist.");
100 }
101
102 if (!directory.isDirectory()) {
103 throw new IllegalArgumentException("File '"
104 + directory.getAbsolutePath() + "' is not a directory.");
105 }
106
107 if (jarFile.exists()) {
108 throw new IllegalArgumentException("Jar file '"
109 + jarFile.getAbsolutePath() + "' already exists.");
110 }
111
112 File files[] = directory.listFiles();
113 String args[] = new String[3 + files.length];
114 args[0] = "jar";
115 args[1] = "cf";
116 args[2] = jarFile.getAbsolutePath();
117 for (int i = 0; i < files.length; i++) {
118 args[3 + i] = files[i].getName();
119 }
120
121 CommandRunner cr = new CommandRunner();
122
123
124 cr.run_command(args, directory, 60);
125 return jarFile;
126 }
127 }