2

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 ?

Jince Martin
  • 301
  • 1
  • 9
  • 18
  • possible duplicate of [Java: Running cmd commands (multiple commands with multiple arguments at once)](http://stackoverflow.com/questions/17072849/java-running-cmd-commands-multiple-commands-with-multiple-arguments-at-once) – ha9u63a7 Jan 08 '15 at 13:57
  • @ha9u63ar : The solution is not appropriate . I can't use batch file for my application – Jince Martin Jan 08 '15 at 14:03
  • 1
    See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. – Andrew Thompson Jan 09 '15 at 01:15

1 Answers1

3

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();
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • it is showing A problem occurred evaluating project ':ECommerce'. > No signature of method: java.lang.String.positive() is applicable for argument types: () values: [] Possible solutions: notify(), size(), tokenize(), size(), tokenize() – Jince Martin Jan 09 '15 at 05:00
  • i need to open cmd also , and need to run this command in that cmd – Jince Martin Jan 09 '15 at 05:01
  • @JinceMartin I'm fairly certain you can't do that. You might write a cmd script and run that, but you can't open and control cmd. – Elliott Frisch Jan 09 '15 at 05:02
  • The answer you provided is good one. But it is showing this error while trying to run- Cannot run program "jboss-cli.bat --connect --command="deploy --force C:\Users\me\git\test\Test\build\libs\TestEAR.ear: CreateProcess error=2, The system cannot find the file specified. I think we have to pass parameters in processbuilder in a different way. Please help me out – Jince Martin Jan 13 '15 at 05:19