I used the following code to execute simple OS command on Windows:
public class Ping { public static void main(String[] args) throws IOException { String command = "ping google.com"; Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); System.out.println(); System.out.println("Finished");
How to modify the code to insert multiple commands instead of one, so let us say I want to ping google.com, and then ping yahoo.com after that. I tried to create array string like:
String [] command = {"ping google.com", "ping yahoo.com"};
However, this showed me an error.
I appreciate your help on this.
Advertisement
Answer
Use a loop:
String [] commands = {"ping google.com", "ping yahoo.com"}; for(String command: commands) { Process process = Runtime.getRuntime().exec(command); //more stuff }