1

I need to execute some line command in my java program. For example, I want to go to a directory and then create a folder in it like this:

cd C:\\Users\\qi11091\\Documents\\TITAN_Command_Line\\FirstTest
mkdir toto

My probem is that I can do the first command, it works but I don't know how to do the second command in my Program.

Here is my code

public void TtcnToC(String path){
    System.out.println("Début du programme");
    try{
        System.out.println("Path target verification: "+path);

        String[] mkdir = {"cmd.exe", "/c","cd C:\\Users\\qi11091\\Documents\\TITAN_Command_Line\\FirstTest", "mkdir titi"};
        String[] mkdir1 = {"cmd.exe", "/c","cd "+ path};
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(mkdir1);

        //Process process1 = runtime.exec(mkdir1);
        //Process process2 = runtime.exec(mkdir2);

        BufferedReader output = getOutput(process);
        BufferedReader error = getError(process);
        String ligne = "";

        while ((ligne = output.readLine()) != null) {
            System.out.println(ligne);
        }

        while ((ligne = error.readLine()) != null) {
            System.out.println(ligne);
        }

        System.out.println("in the shell");
        process.waitFor();
    }
Martin G
  • 17,357
  • 9
  • 82
  • 98
Anatch
  • 443
  • 1
  • 5
  • 20
  • what exactly are you using Process and runtime for all this? The File class has everything you need to create new folders and such. – Stultuske Apr 15 '15 at 08:37
  • It is just an example so I can understand how to work with command line on a Java programm. I just need to know how to write several line commands in a java programm, because I will need it to write others commands than creating a folder – Anatch Apr 15 '15 at 08:40

2 Answers2

1

To execute multiple commands in succession you can execute them in a single command by using the syntax that command line uses to execute multiple commands in the same line.

You can create a private method that concatenates all of these commands into one command that CMD will understand:

private String combineCommands(String[] commands){
    String result = "";
    for(String command: commands){
        result = result + command + " && ";
    }
    if(result.length() > 0){
        result = result.subString(0,result.length()-3); // We remove the last && here.
    }
    return result;
}

So you pass it a String array, for example:

String[] myCommands = {"cd C:\\Users\\qi11091\\Documents\\TITAN_Command_Line\\FirstTest", "mkdir titi"};

Then you can just simply call your execution as you did before:

String[] mkdir1 = {"cmd.exe", "/c",combineCommands(myCommands)};
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(mkdir1);

This way you don't have to make a call to Runtime for every command you wish to execute.

Ceiling Gecko
  • 3,104
  • 2
  • 24
  • 33
0

Just for a sample, you can have a look at the below code,

    try { 

        String[] command = new String[2];
        command[0]="cmd /c dir";
        command[1]="cmd /c dir/w";

        Process p;

        for (int i = 0; i < command.length; i++) {
            String cmd = command[i];
            p=Runtime.getRuntime().exec(cmd); 
            p.waitFor();    

            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
            String line=reader.readLine(); 
            while(line!=null) 
            { 
                System.out.println(line); 
                line=reader.readLine(); 
            } 
        }

    } catch(IOException e1) {
        e1.printStackTrace();
    } 
    catch(InterruptedException e2) {
        e2.printStackTrace();
    } 

    System.out.println("Done"); 
}
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57