I am using Java ProcessBuilder to execute two shell scripts. I want to have a 10 seconds time gap between two executions. So that's why I am using Threads. But following code doesn't execute shell scripts( without threads they execute).
public void execute() {
new Thread(new Runnable() {
public void run() {
Process process_1;
try {
List<String> cmdList_1 = new ArrayList<String>();
cmdList_1.add("sh");
cmdList_1.add("command_1.sh");
ProcessBuilder pb = new ProcessBuilder(cmdList_1);
process_1 = pb.start();
process_1.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
Process process_2;
try {
Thread.sleep(10000);
List<String> cmdList_2 = new ArrayList<String>();
cmdList_2.add("sh");
cmdList_2.add("command_2.sh");
ProcessBuilder pb = new ProcessBuilder(cmdList_2);
process_2 = pb.start();
process_2.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
How can I fix my code ? or are there any better way to fulfill my ambition?
Thanks in advance
Try consuming the output from the process, something like this:
ProcessBuilder processBuilder = new ProcessBuilder();
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader((new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.