In Java, how to insert multiple OS commands line?

Tags: , , , ,

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";

        Process process = Runtime.getRuntime().exec(command);

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {



How to modify the code to insert multiple commands instead of one, so let us say I want to ping, and then ping after that. I tried to create array string like:

String [] command = {"ping", "ping"};

However, this showed me an error.

I appreciate your help on this.


Use a loop:

String [] commands = {"ping", "ping"};
for(String command: commands) {
     Process process = Runtime.getRuntime().exec(command);
     //more stuff

Source: stackoverflow