-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerExecHandler.java
More file actions
37 lines (33 loc) · 1.03 KB
/
Copy pathServerExecHandler.java
File metadata and controls
37 lines (33 loc) · 1.03 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ServerExecHandler implements ServerRequestHandler {
private boolean includeArgs;
private String command;
public ServerExecHandler(String command, boolean includeArgs) {
this.includeArgs = includeArgs;
this.command = command;
}
@Override
public String handle(String args) {
String command = this.command;
if (includeArgs) {
command = String.format("%s %s", command, args);
}
try {
Process process = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String result = "";
String output;
while ((output = br.readLine()) != null) {
result += output + "\n";
}
return result;
}
catch (IOException e) {
String message = String.format("Error while running \"%s\": %s\n", command, e.getMessage());
System.err.println(message);
return message;
}
}
}