39

I need to exec the following command from ant, but I can't figure out how to escape the double-quotes:

tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
sachin
  • 665
  • 3
  • 8
  • 15

5 Answers5

68

Ant uses XML, so you can use the normal XML entities like ":

tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
27

Ant script is xml. So in xml, here is the rule.

For > use >

For < use &lt;

For “ use &quot;

For & use &amp;

For ‘ use &apos;

Notice! ";"

Reference:

http://www.jguru.com/faq/view.jsp?EID=721755

alseether
  • 1,889
  • 2
  • 24
  • 39
user890054
  • 378
  • 3
  • 4
20

I don't believe you really do if you use <arg value> and not <arg line>:

tasklist /FI "IMAGENAME eq java.exe" /FI "MEMUSAGE gt 50000"

<exec executable="tasklist">
    <arg value="/FI"/>
    <arg value="IMAGENAME eq java.exe"/>
    <arg value="/FI"/>
    <arg value="MEMUSAGE gt 50000"/>
</exec>

Despite the spaces, the <arg value> will send it as a single parameter to the command. Unless the command itself requires quotes, this should work.

David W.
  • 105,218
  • 39
  • 216
  • 337
  • 9
    The answer does not match the question title. – cmcginty Jan 10 '14 at 22:49
  • True. This doesn't answer the question the way the OP wanted to. But, using ``, the OP no longer needs the quotes. The quotes were to keep the parameters with spaces together. This does the same thing. If you need a quote, you need to use `"` which isn't as elegant. – David W. Jan 12 '14 at 02:15
7

But doesn't work if you need to use the find DOS command in a /CMD exec task:

<target name="install" depends="install2">
    <exec executable="cmd.exe" outputproperty="result.process">
        <arg line='/c tasklist | find "httpd"'/>
    </exec>
    <echo message="RESULT: ${result.process}" />
</target>

gives,

install:
     [exec] Current OS is Windows 7
     [exec] Output redirected to property: result.process
     [exec] Executing 'cmd.exe' with arguments:
     [exec] '/c'
     [exec] 'tasklist'
     [exec] '|'
     [exec] 'find'
     [exec] 'httpd'
     [exec]
     [exec] The ' characters around the executable and arguments are
     [exec] not part of the command.
     [exec] Result: 2
     [echo] RESULT: FIND : format incorrect de paramètre

Its like if ANT deletes the double quotes around the parameter when it is passed to the CMD interpereter. The help for the find DOS function says you need to use double quotes for the text you're looking for.

Matthieu
  • 2,736
  • 4
  • 57
  • 87
sqaopen
  • 71
  • 1
  • 1
3

Here is an example http://ant.apache.org/faq.html#shell-redirect-2. Simply use single quotes as xml parameter separator. This way you could freely use double quotes inside the arguments.

Alexander Sulfrian
  • 3,533
  • 1
  • 16
  • 9