1 | package net.oni2.aeinstaller.backend.appexecution;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Vector;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @author Christian Illy
|
---|
13 | */
|
---|
14 | public class AppExecution {
|
---|
15 | /**
|
---|
16 | * Execute a short running application and wait for termination
|
---|
17 | *
|
---|
18 | * @param cmdLine
|
---|
19 | * List of command and arguments
|
---|
20 | * @return Error code and list of output lines
|
---|
21 | * @throws IOException
|
---|
22 | * Exc
|
---|
23 | */
|
---|
24 | public static AppExecutionResult executeAndWait(Vector<String> cmdLine)
|
---|
25 | throws IOException {
|
---|
26 | ProcessBuilder pb = new ProcessBuilder(cmdLine);
|
---|
27 | pb.redirectErrorStream(true);
|
---|
28 | Process proc = pb.start();
|
---|
29 |
|
---|
30 | InputStream is = proc.getInputStream();
|
---|
31 | InputStreamReader isr = new InputStreamReader(is);
|
---|
32 | BufferedReader br = new BufferedReader(isr);
|
---|
33 |
|
---|
34 | String line;
|
---|
35 | Vector<String> lines = new Vector<String>();
|
---|
36 |
|
---|
37 | while ((line = br.readLine()) != null) {
|
---|
38 | lines.add(line);
|
---|
39 | }
|
---|
40 | try {
|
---|
41 | proc.waitFor();
|
---|
42 | } catch (InterruptedException e) {
|
---|
43 | // TODO Auto-generated catch block
|
---|
44 | e.printStackTrace();
|
---|
45 | }
|
---|
46 | return new AppExecutionResult(proc.exitValue(), cmdLine, lines);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Execute an app without waiting
|
---|
51 | *
|
---|
52 | * @param cmd
|
---|
53 | * Command and parameters
|
---|
54 | * @param workingDirectory
|
---|
55 | * Working directory of app
|
---|
56 | */
|
---|
57 | public static void execute(List<String> cmd, File workingDirectory) {
|
---|
58 | try {
|
---|
59 | ProcessBuilder pb = new ProcessBuilder(cmd);
|
---|
60 | pb.directory(workingDirectory);
|
---|
61 | pb.start();
|
---|
62 | } catch (IOException e) {
|
---|
63 | e.printStackTrace();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|