gradle-plugins icon indicating copy to clipboard operation
gradle-plugins copied to clipboard

Exec plugin: Command string with spaces does not work on mac os x

Open bvanberg opened this issue 14 years ago • 6 comments

Tried using this today to execute a command on Mac OS X. It only works when the command string does not contain spaces. The example did not work..."ls -al" where "ls" does. I believe this has to do with how Java treats the command line string when passed in as an array. i.e. if running with Java directly

Runtime.getRuntime().exec("ls -al", null, new File("");

This does not work. However, if the array is broken up as follows

Runtime.getRuntime().exec("ls, "-al", null, new File("");

This does work. It works because the method being called is expecting a string array that is already broken up into tokens that are the command followed by the arguments.

bvanberg avatar Feb 21 '11 23:02 bvanberg

ExecPlugin.groovy: Line 13

  project.execIn(baseDir, cmd)

I think if cmd is split on spaces at this point it will fix the issue.

bvanberg avatar Feb 21 '11 23:02 bvanberg

What if people want to pass a command that contains spaces?

RobertFischer avatar Feb 22 '11 01:02 RobertFischer

Do you mean that, perhaps, the command has spaces in it's path like this?

C:/Command Path/command

I think this is ok, but in this case the best approach would be to separate the command from the arguments from the get go, and not use a single string for the command and arguments like this.

C:/Command Path/command -arg1 -arg2 arg3

Because there isn't a good way to know which spaces are a part of the command, and which delimit arguments. In this case I would use ProcessBuilder.start() rather than Runtime.getRuntime().exec directly. Check out ProcessBuilder and let me know what you think.

bvanberg avatar Feb 22 '11 16:02 bvanberg

I know about ProcessBuilder, but there was an issue with slurping I/O, IIRC.

RobertFischer avatar Feb 22 '11 16:02 RobertFischer

Looks like the 1.7 release of ProcessBuilder fixes the issue. I'll shift over to using that.

RobertFischer avatar Mar 07 '11 20:03 RobertFischer

In JBoss Tools ran into this issue in a different context) and stumbled upon your issue. Using MacOS 10.12.4 and java 1.8.0_111 this doesnt seem to be fixed afaics.

adietish avatar May 17 '17 10:05 adietish