Jince Martin
Jince Martin

Reputation: 311

execute multiple commands in cmd using java

I need to execute multiple comments in single cmd window using java.

The comments are

1. cd C:\Apps\wildfly-8.0.0.Final\bin
2. jboss-cli.bat --connect --command=\"deploy --force C:\Users\me\git\test\Test\build\libs\TestEAR.ear

Because I need to execute the second command from the folder "C:\Apps\wildfly-8.0.0.Final\bin".

I tried this :

Runtime.getRuntime().exec("cmd /c start cd C:\\Apps\\wildfly-8.0.0.Final\\bin\\ && start cmd.exe /c jboss-cli.bat --connect --command=\"deploy --force C:\\Users\\me\\git\\test\\Test\\build\\libs\\TestEAR.ear\"");

But it is executing these commands separate , that is it will open one cmd window and executes the first commands , then it will execute the second command in another cmd window , and showing the error :

Could not locate "C:\Users\me\git\test\Test\build\libs\TestEAR.ear".
Please check that you are in the bin directory when running this script.
Press any key to continue . . .

I found some solutions with batch file , but in my application I can't use batch file (must not use batch file ) .

Can anyone suggest a solution ?

Upvotes: 2

Views: 4853

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201507

If I understand your question you could use a ProcessBuilder and call directory(File). Something like

public static void main(String[] args) throws IOException {
    String folder = "C:\\Apps\\wildfly-8.0.0.Final\\bin";
    String command = "jboss-cli.bat --connect --command=\"deploy --force "
        + "C:\\Users\\me\\git\\test\\Test\\build\\libs\\TestEAR.ear\"";
    ProcessBuilder pb = new ProcessBuilder(command);
    pb.directory(new File(folder));
    pb.inheritIO();
    Process p = pb.start();
    try {
        p.waitFor();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Upvotes: 3

Related Questions