0

I write some java code in order to execute some external commands in the mac terminal. The original command to run in the terminal is

xpl-sender -c plugwise.basic -m xpl-cmnd command=livepower device=B83229 -w 3

It is really weird because if I use the command string array likes this:

Process ls = Runtime.getRuntime().exec(new String[]{
   "/usr/local/bin/xpl-sender",
   "-c", "plugwise.basic",
   "-m", "xpl-cmnd",
   "command=" + command,
   "device=" + id});

Without "-w 3", thing works normally but of course it lacks of parameters.

But if i use like this

Process ls = Runtime.getRuntime().exec(new String[]{
   "/usr/local/bin/xpl-sender",
   "-c", "plugwise.basic",
   "-m", "xpl-cmnd",
   "command=" + command,
   "device=" + id,
   "-w 3"});

With "-w 3" at the end, nothing works anymore, the command seems that it isn't executed.

And if I use like this

Process ls = Runtime.getRuntime().exec(new String[]{
   "/usr/local/bin/xpl-sender",
   "-c", "plugwise.basic",
   "-m", "xpl-cmnd",
   "command=" + command,
   "device=" + id + " -w 3"});

With +" -w 3"at the end, the perl program which is used to executed the command will return

Illegal hexadecimal digit '-' ignored at /Library/Perl/5.12/xPL/Dock/Plugwise.pm line 964.

It seems that the error caused by the space between -w and 3....
Can someone show me what i did wrong here?
Thank you very much.

4

2 回答 2

1

Due to the way the Runtime interprets arguments, you need to split the string up: "-w" and "3", not "-w 3". The designers chose to implement this behavior because there are some cases where you want to specify arguments with spaces in them. E.g:

grep -r -e "Where are they now" .

Rather than making developers have to manually escape the string "Where are they now", in the example above, the Runtime allows for one to simply pass the entire string as an argument, which then gets passed to the underlying process. It does mean however that in cases like this:

ls -al ~

That each argument needs to be passed to the Runtime separately. In the above example if you passed in one argument as "ls -al", it would actually be passed to the underlying process as one string, and of course, the process would fail because there is no existing binary with that name.

于 2013-03-23T21:50:24.560 回答
1

You are doing the equivalent of shell command

xpl-sender ... '-w 3'   # One 4-char arg

instead of

xpl-sender ... -w 3     # Two args

You want:

Process ls = Runtime.getRuntime().exec(new String[]{
   "/usr/local/bin/xpl-sender",
   "-c", "plugwise.basic",
   "-m", "xpl-cmnd",
   "command=" + command,
   "device=" + id,
   "-w", "3"});

Since most programs don't accept options after non-options (although you report it works for your xml-sender), and because it looks better, I'd move the -w up.

Process ls = Runtime.getRuntime().exec(new String[]{
   "/usr/local/bin/xpl-sender",
   "-c", "plugwise.basic",
   "-m", "xpl-cmnd",
   "-w", "3",
   "command=" + command,
   "device=" + id});
于 2013-03-23T21:51:05.480 回答