Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java 执行 shell 脚本
public class ReloadSystem {
public static void reloadSystem() {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("bash", "-c", "bash ~/tieba-demo/Tieba/reload.sh");
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
System.out.println(output);
return;
} else {
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.