Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running bat file with java processbuilder

I am trying to execute .bat file using java process builder but it does not starts the process. Please tell me what i am doing wrong here. This code works fine with linux envoirnment when I replace file.bat with ./file.sh

final ArrayList<String> command = new ArrayList<String>();
command.add(WORKING_DIR+File.separator+"file.bat");
final ProcessBuilder builder = new ProcessBuilder(command);
try {
    builder.redirectErrorStream(true);
    builder.start();
    } catch (IOException e) {
      logger.error("Could not start process." ,e);
} 
like image 674
Salman Raza Avatar asked Feb 16 '23 05:02

Salman Raza


1 Answers

First element in array must be an executable. So you have to invoke cmd.exe in order to call you batch file.

ProcessBuilder builder = new ProcessBuilder(Arrays.asList(new String[] {"cmd.exe", "/C", WORKING_DIR + File.separator + "file.bat"}));
like image 126
Toilal Avatar answered Feb 23 '23 16:02

Toilal