-
Notifications
You must be signed in to change notification settings - Fork 3
/
RunHelper.java
58 lines (46 loc) · 1.66 KB
/
RunHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.andresoviedo.util.run;
import java.util.Arrays;
import org.andresoviedo.util.io.IOHelper.StreamGobbler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public final class RunHelper {
private static final Log logger = LogFactory.getLog(RunHelper.class);
public static int exec(String file, Appendable output, String... args) throws Exception {
String[] cmdArray = new String[args != null ? args.length + 1 : 1];
cmdArray[0] = file;
if (args != null) {
System.arraycopy(args, 0, cmdArray, 1, args.length);
}
return exec(cmdArray, output);
}
public static int execBat(String batFile, Appendable output, String... args) throws Exception {
String[] cmdArray = new String[3];
cmdArray[0] = "cmd.exe";
cmdArray[1] = "/c";
cmdArray[2] = "\"";
cmdArray[2] += "\"" + batFile + "\"";
if (args != null) {
for (String arg : args) {
if (arg.contains(" ")) {
cmdArray[2] += " \"" + arg + "\"";
} else {
cmdArray[2] += " " + arg;
}
}
}
cmdArray[2] += "\"";
return exec(cmdArray, output);
}
public static int exec(String[] cmdArray, Appendable output) throws Exception {
logger.info("Executing " + Arrays.toString(cmdArray) + "'...");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmdArray);
StreamGobbler stdOutReader = new StreamGobbler(pr.getInputStream(), "STDOUT", output);
StreamGobbler stdErrReader = new StreamGobbler(pr.getErrorStream(), "STDERR");
stdOutReader.start();
stdErrReader.start();
int status = pr.waitFor();
logger.info("Finished with status '" + status + "'.");
return status;
}
}