sachin
sachin

Reputation: 685

How can I escape double-quotes in ant?

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"

Upvotes: 40

Views: 48042

Answers (5)

user890054
user890054

Reputation: 394

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

Upvotes: 29

sqaopen
sqaopen

Reputation: 71

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.

Upvotes: 7

David W.
David W.

Reputation: 107040

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.

Upvotes: 20

Michael Mrozek
Michael Mrozek

Reputation: 175365

Ant uses XML, so you can use the normal XML entities like &quot;:

tasklist /FI &quot;IMAGENAME eq java.exe&quot; /FI &quot;MEMUSAGE gt 50000&quot;

Upvotes: 71

Alexander Sulfrian
Alexander Sulfrian

Reputation: 3563

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.

Upvotes: 3

Related Questions