ProcessBuilder可以执行与操作系统进行交互的命令,现在给出示例来使用这个类:
ProcessBuilder processBuilder;
try {
processBuilder =
new ProcessBuilder(
Arrays.asList(
"python3",
"/usr/local/bin/SetName.py",
id));
Process process = processBuilder.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
log.info("autoRefreshHostname Python script output: {}", line);
}
int exitCode = process.waitFor();
log.info("Python script exited with code: {}", exitCode);
} catch (Exception e) {
log.error("Error while executing autoRefreshHostname Python script", e);
}