简体   繁体   中英

Java Runtime.exec escaped arguments in string

Due to working through a framework, I only have control over the command string of Runtime.getRuntime().exec(string) , so no array.

Problem is, I need to pass some escaped arguments and it just doesn't seem to work.

Take this for example: wget -qO- --post-data "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Devices><Device><FLOWCTRL>2</FLOWCTRL></Device></Devices>" -- http://192.168.3.33/data/changes.xml . Works perfectly fine in the shell, but something is messed up since I don't get the proper response (most probably because the data isn't valid).

Edit: https://github.com/openhab/openhab-addons/blob/2.5.x/bundles/org.openhab.binding.exec/src/main/java/org/openhab/binding/exec/internal/handler/ExecHandler.java#L174 Link to code

As I said, I have no control over this... I need to do it in one string :(

There is no direct solution given that constraint. Period.

It is a plain fact that exec(String) does not understand any form of escaping or quoting. It splits the string into a command name and multiple arguments using whitespace characters as the argument separator. The behavior is hard-wired ... and documented.


The possible solutions are:

  • Do the splitting yourself, and use exec(String[]) .
  • Get a shell to do the splitting; eg

    String cmd = "wget -qO- --post-data \\"<?xml version=\\\\"1.0\\\\" ...." Runtime.getRuntime().exec("/bin/sh", "-c", cmd);

    Note that we are using exec(String[]) here too.

  • Generate and run a shell script on the fly:

    1. Write the following script to a temporary file (say "/tmp/abc.sh")

       #!/bin/sh wget -qO- --post-data \\ "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><Devices><Device><FLOWCTRL>2</FLOWCTRL></Device></Devices>" \\ -- http://192.168.3.33/data/changes.xml
    2. Make the script executable

    3. Run it:

       Runtime.getRuntime().exec("/tmp/abc.sh");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM