Repository navigation
Expand file tree
/
Copy pathShell.java
More file actions
83 lines (66 loc) · 2.11 KB
/
Copy pathShell.java
File metadata and controls
83 lines (66 loc) · 2.11 KB
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
74
75
76
77
78
79
80
81
82
83
package ev3dev.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Command line wrapper
*
* @author Juan Antonio Breña Moral
*/
@Slf4j
public class Shell {
public static String COMMAND_ERROR_MESSAGE = "COMMAND_ERROR";
/**
* Execute a command passed as a parameter
*
* @param command Command to execute in Linux
* @return Result from the command
*/
public static String execute(final String command) {
LOGGER.debug("Command: {}", command);
StringBuilder output = new StringBuilder();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
reader.close();
} catch (IOException | InterruptedException e) {
LOGGER.warn(e.getLocalizedMessage(), e);
return COMMAND_ERROR_MESSAGE;
}
return output.toString();
}
/**
* Execute a set of commands passed as a parameter
*
* @param command Command to execute in Linux
* @return Result from the command
*/
public static String execute(final String[] command) {
for (String cmd : command) {
LOGGER.info("Command chunk: {}", cmd);
}
StringBuilder output = new StringBuilder();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
reader.close();
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
e.printStackTrace();
}
return output.toString();
}
}