Question

I'm having trouble running a simple bash script from Java. Specifically:

...

try{
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", command);
pb.directory(new File(dir));
Process shell = pb.start();
int exitVal = shell.waitFor();

... where 'command' the absolute path to a bash script that is executable by all and 'dir' is the working directory.

When I run my program I get an exit code 127 ("command not found"). I've tried using the Java Runtime class and the process.exec method but neither have worked for me. Any suggestions?

Was it helpful?

Solution

If "command" is a bash script, then instead of passing "/bin/bash" (and the erroneous "-c" like you're doing) to ProcessBuilder, just make sure that command is executable (chmod +x command), that the first line is #!/bin/bash, and then pass the full path to it into ProcessBuilder.

OTHER TIPS

No -c. That means the script is the argument to -c. You are passing it a pathname, and you don't use -c for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top