Search code examples
javacmdcommandprocessbuilder

Processbuilder: Cannot combine 2 paths as argument to execute app with a macro


So for work I would like to automate something for minitab. We get results from our microscope and these need to be put into Minitab. Now I wanted to make a program that does some changes to the text file and then automatically opens minitab with a macro. I have everything working except for the auto opening of the macro with minitab.

I can launch it from cmd manually no problem, so it should be working.

Code can be found below, after compiling and running I get this error

'C:/Program' is not recognized as an internal or external command, operable program or batch file.

Process finished with exit code 0

Which makes me believe cmd does something like:

cmd.exe,/c,c:/Program,Files/..

instead of

cmd.exe,/c,c:/program files/...

        String PathExe = "\"C:/Program Files/Minitab/Minitab 17/Minitab 17/Mtb.exe\"";
        String Macro = "\"c:/minitAPP/Import.mtb\"";

        ProcessBuilder builder;
        builder = new ProcessBuilder("cmd.exe", "/c", PathExe + " " + Macro);
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);

Solution

  • There is no need to use cmd.exe to execute another .exe file. Just execute it directly, without the quotes:

    ProcessBuilder builder = new ProcessBuilder(
        "C:\\Program Files\\Minitab\\Minitab 17\\Minitab 17\\Mtb.exe",
        "c:\\minitAPP\\Import.mtb");
    

    By specifying an entire path as a single argument to ProcessBuilder, you ensure that the operating system will treat it as a single argument, which is the purpose of using quotations marks on a normal command line.