Java ProcessBuilder unable to run shell script

  bash, java, windows

I have a shell script which takes in an argument and prints to file Logone.txt:

name=$1
log_file="Logone.txt"

if [[ -n "$name" ]]; then
    echo "$1" >> ${log_file}
else
    echo "argument error"
fi

This works when tested using GitBash . Example below giving 666 as argument.:

 ~/Documents/Documents/rt-wizard/source
  bash pm2_StopStart.bash 666

I am now trying to do the same thing from within Java code using ProcessBuilder.

    try {
        ProcessBuilder pb = new ProcessBuilder("bash", "-c", "pm2_StopStart.bash");
        Process p = pb.start();

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

This gives an error:

Cannot run program "bash": CreateProcess error=2, The system cannot find the file specified

Script pm2_StopStart.bash is located in ~/Documents/Documents/rt-wizard/source and program is also being run from the same directory.

Source: Windows Questions

LEAVE A COMMENT