Java debug options in ant build.xml

To add debug options to java (Sun's JDK) command line:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9009
If the java process runs and terminates itself, change suspend=y, so you can have a chance to attach the remote debugger.

To add the same debug options to ant build.xml java task:
<target name="run">
 <java fork="on"
     <jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009" />
     <arg line="--arg1 arg2 --arg3 arg4"/>
Or, with the newer -agentlib option:
<target name="test" depends="compile">
    <junit printsummary="yes" haltonerror="no" haltonfailure="no" fork="true">
      <formatter type="plain" usefile="false"/>
      <formatter type="xml"/>
      <test name="FooBeanTest"
      <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />
        <path refid="test.compile.classpath"/>
        <path refid="javaee.classpath"/>
        <pathelement path="${build.dir}"/>
        <pathelement path="${junit.jar}"/>
Note: <arg> and <jvmarg> take either line or value attribute, but value attributes are treated as one single argument that may contain spaces. So in our case line attribute is used to specify multiple arguments separated by spaces. If value attribute was used instead of line attribute, the debug options will not take effect.
The same element can also be used on ant testng task to debug test run:
<testng mode="testng" dumpCommand="true" workingDir="." failureProperty="tests.failed" outputdir="results">
        <fileset dir="lib">
            <include name="*.jar" />
    <xmlfileset dir="." includes="testng-suite.xml" />
    <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />


Anonymous said...

Thanks for this post. It was very useful.

Deepthi Nikhil said...

its not working for me. the program stalled wen i used the above code.plz provide code in detail

javahowto said...

your program is waiting for the debugger to be attached. If you don't want it to wait, change suspend to n

Unknown said...

Great and Useful Article.

Online Java Training

Java Online Training India

Java Online Course

Java EE course

Java EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Course in Chennai

Java Online Training India

Unknown said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000

Hel Stone said...

Great that you shared it. I also want to share this source http://myessaywritingblock.com/mypapergeek-net-review/ with others!