1

I'm trying to figure out how to execute a simple bash script from a JAVA program inside eclipse. I have already checked some questions like Cannot run program "..../abc.exe": error=13, Permission denied and Permission denied when running shell script from java program, but still I have this error:

IOException: Cannot run program "prog" error=13, Permission denied.

I have already checked file permissions:

-rwxrwxr-x user user prog

I use this snippet to execute the script:

File file = new File(this.getClass().getClassLoader().getResource("prog").getFile());
ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
try {
  Process process = processBuilder.start();
  process.waitFor();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
2
  • Where is this script? It looks like you may have it inside a jar - objects in a jar are not files and you can't access them using File. Commented Nov 17, 2016 at 17:49
  • It is inside the resources folder of my eclipse projects. The file is a bash script which calls some jars on which I have no control. If I call it via terminal it works perfectly... Commented Nov 17, 2016 at 17:52

1 Answer 1

2

When invoking bash scripts from java you must specify '/bin/bash' as the executable and pass your script path as an argument.

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

1 Comment

I tried in the following manner: 'ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", file.getAbsolutePath());' and the script gets executed. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.