views:

40

answers:

2

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?

A: 

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.

bmargulies
A: 

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.

Paul Tomblin
Thanks! Got it working
akobre01