I'd like to invoke bash using a string as input. Something like:
sh -l -c "./foo"
I'd like to do this from Java. Unfortunately, when I try to invoke the command using getRuntime().exec
, I get the following error:
foo": -c: line 0: unexpected EOF while looking for matching `"'
foo": -c: line 1: syntax error: unexpected end of file
It seems to be related to my string not being terminated with an EOF.
Is there a way to insert a platform specific EOF into a Java string? Or should I be looking for another approach, like writing to a temp script before invoking "sh" ?
Use this:
Runtime.getRuntime().exec(new String[] {"sh", "-l", "-c", "./foo"});
Main point: don't put the double quotes in. That's only used when writing a command-line in the shell!
e.g., echo "Hello, world!"
(as typed in the shell) gets translated to:
Runtime.getRuntime().exec(new String[] {"echo", "Hello, world!"});
(Just forget for the moment that the shell normally has a builtin for echo
, and is calling /bin/echo
instead. :-))