7/14/2010

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"
       failonerror="true"
       classpath="xxx"
       classname="xxx">
     <jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009" />
     <arg line="--arg1 arg2 --arg3 arg4"/>
  </java>
</target>
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"
          todir="xxx"/>
      <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />
      <classpath>
        <path refid="test.compile.classpath"/>
        <path refid="javaee.classpath"/>
        <pathelement path="${build.dir}"/>
        <pathelement path="${junit.jar}"/>
      </classpath>
    </junit>
  </target>
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">
    <classpath>
        <fileset dir="lib">
            <include name="*.jar" />
        </fileset>
    </classpath>
    <xmlfileset dir="." includes="testng-suite.xml" />
    <jvmarg line="-agentlib:jdwp=transport=dt_socket,address=localhost:9009,server=y,suspend=y" />
</testng>

4 comments:

Anonymous said...

Thanks for this post. It was very useful.

deepu 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

Steve Smith 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