public static String runcmd(String cmdline) {
// String userpswd = SystemConfiguration.getValue("userpswd");
// String cmd = "echo " +"\"" + userpswd + "\"" + " | sudo reboot";
// String cmd = "ps -aux | grep java";
// String cmd = "sudo reboot";
String cmd = cmdline;
List<String> command = new ArrayList<>();
command.add("/bin/sh");// this is good
command.add("-c");
command.add(cmd);
try {
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
LineNumberReader ir = new LineNumberReader(
new InputStreamReader(process.getInputStream(), Charset.forName("UTF8")));
String line;
StringBuilder sb = new StringBuilder();
while ((line = ir.readLine()) != null) {
sb.append(line);
}
if (process.isAlive()) {
process.waitFor();
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}