Java.io.IOException: Cannot run program “”: CreateProcess error=2, The system cannot find the file specified

javaprocessbuildersh

Im just trying to test running a shell script thats in my project directory in Eclipse.

new ProcessBuilder("scripts/test.sh").start();

enter image description here

Getting this error:

java.io.IOException: Cannot run program "scripts/test.sh": CreateProcess error=2, The system cannot find the file specified

Best Answer

This could be for two reasons:

  • Java execute a system/exec C routine, which except a binary. test.sh is not a binary. You should probably use bash: bash -f scripts/test.sh -> new ProcessBuilder()("bash", "-f", new File("scripts/test.sh").getAbsoluteFile());
  • The file scripts/test.sh does not exists, meaning the current directory is not good.

You can try System.out.println(new File("scripts/test.sh").getAbsoluteFile()) to print the path Java is using.

Related Topic