1

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

1
  • 5
    Well, if it can't find the file, it would be a good idea to try logging the current working directory... Commented Aug 26, 2014 at 14:16

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Was trying to run bash script from Java application in windows and getting same error. getAbsoluteFile() enabled JVM to get the bash. Obviously, I installed Cygwin bash previously to get the absolute bash.exe file path in Windows.
String cmd="scripts/"+scriptName; String scriptFile = new File(cmd).getAbsoluteFile().getAbsolutePath(); String testcaseNo= fileName.split("\\.")[0]; try { String bashFile = PropertyFile.bashFile; ProcessBuilder processBuilder=new ProcessBuilder(bashFile,"-f",scriptFile + " " + arg0 + " " + arg1); processBuilder.redirectErrorStream(true); Process bashProcess=processBuilder.start(); bashProcess.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
The above code worked for me. The reason as @NoDataFound stated is bash file not being found by Java

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.