Reputation: 25
I am running a shell script(ksh with shebang at top of script) from my java program using ProcessBuilder:
processBuilder.command("/bin/sh","-c"," . " + /somepath/script.ksh + " " + argument);
proc = processBuilder.start();
Everything works fine using my java program. I wanted to run the command on the command line and figured the command being run from the java program was:
/bin/sh -c . /somepath/script.ksh argument
However, that does not work on the command line and I receive this error:
/somepath/script.ksh: line 0: .: filename argument required
.: usage: . filename [arguments]
Looks like the "-c" flag is expecting the next item to be a command(which in my statement is the source operator "."), and after that it is expecting more arguments. So my script(/somepath/script.ksh) is being taken as argument instead of a command. Why does it work with processbuilder in the java program? Is it creating the command differently?
Upvotes: 2
Views: 1348
Reputation: 168845
Change:
processBuilder.command("/bin/sh","-c"," . " + /somepath/script.ksh + " " + argument);
To
processBuilder.command("/bin/sh","-c","./somepath/script.ksh", argument);
Give all the parts of the String[]
as a separate String
.
Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec
and (continue to) build the Process
using a ProcessBuilder
.
Upvotes: 0
Reputation: 123628
The -c
option to /bin/sh
would make sh
interpret the argument as a command line. So instead of saying:
/bin/sh -c . /somepath/script.ksh argument
you'd need to say:
/bin/sh -c ". /somepath/script.ksh argument"
Alternatively, you could say:
/bin/sh /somepath/script.ksh argument
Upvotes: 2