-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimployCommandRunner.java
73 lines (56 loc) · 1.67 KB
/
SimployCommandRunner.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.IOException;
import java.io.InputStream;
public class SimployCommandRunner {
@SuppressWarnings("deprecation")
static String exec(String command) throws Exception {
final Process process = startProcess(command);
final StringBuilder stdout = new StringBuilder();
Thread capturing = new Thread() { public void run() {
try {
capture(process.getInputStream(), stdout);
} catch (ThreadDeath td) {
//Reading from the inputStream above will block sometimes when the process ends. :(
} catch (Exception e) {
e.printStackTrace();
}
}};
capturing.start();
waitForExit(process);
sleepASecond();
capturing.stop(new ThreadDeath());
if (process.exitValue() != 0)
throw new Exception("Command failed: " + command);
System.out.println("\n");
return stdout.toString();
}
private static void sleepASecond() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static Process startProcess(String command) throws IOException {
System.out.println("Executing: " + command);
ProcessBuilder builder = new ProcessBuilder();
builder.command(command.split(" "));
builder.redirectErrorStream(true);
return builder.start();
}
private static void capture(InputStream inputStream, StringBuilder stdout) throws Exception {
while (true) {
int read = inputStream.read(); //This will block forever sometimes when the process ends. :(
if (read == -1)
return;
stdout.append((char)read);
System.out.print((char)read);
}
}
private static void waitForExit(Process process) {
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}