How do I run a batch file from my Java Application?

后端 未结 11 1853
情书的邮戳
情书的邮戳 2020-11-22 00:42

In my Java application, I want to run a batch file that calls \"scons -Q implicit-deps-changed build\\file_load_type export\\file_load_type\"

It seems t

11条回答
  •  我寻月下人不归
    2020-11-22 01:09

    Batch files are not an executable. They need an application to run them (i.e. cmd).

    On UNIX, the script file has shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by Windows Explorer. CreateProcess does not know anything about that.

    Runtime.
       getRuntime().
       exec("cmd /c start \"\" build.bat");
    

    Note: With the start \"\" command, a separate command window will be opened with a blank title and any output from the batch file will be displayed there. It should also work with just `cmd /c build.bat", in which case the output can be read from the sub-process in Java if desired.

提交回复
热议问题