If we execute the Junit test cases in Ant (most of the time we do) and some cases fail, we may expect to debug Java code in Ant to analyze the failures. This tutorial will demonstrate how to remote debug Java code in ant and Eclipse.

1. Open the ant build.xml, find the Java execution task java, add the following nested elements jvmarg to specify arguments for the forked VM, it will launch the java program in ant with debugging turned on mode, and the remote debugger port will be ready on port 5555 to listen collections.

<jvmarg value="-Xdebug"/>
<jvmarg value="-Xnoagent"/>
<jvmarg value="-Xrunjdwp:transport=dt_socket,address=5555,server=y,suspend=y"/>
If the value is y, the target application listens for a debugger application to attach. Otherwise, it attaches to a debugger application at the specified address.
This is the transport address for the connection. If the server is n, attempt to attach to a debugger application at this address. Otherwise, listen for a connection at this port.
If the value is y, the target VM will be suspended until the debugger application connects.

Here goes a whole examples:

<java classname="${test.case}" fork="yes" failonerror="false" maxmemory="512m">
  <jvmarg value="-Xdebug"/>
  <jvmarg value="-Xnoagent"/>
  <jvmarg value="-Xrunjdwp:transport=dt_socket,address=5556,server=y,suspend=y"/>
  <arg value="${src.dir}"/>
  <classpath refid="app.classpath"/>
    <pathelement path="${java.dir}/lib/tools.jar"/>
    <pathelement path="${oc4j.dir}/j2ee/home/oc4j.jar"/>
    <pathelement path="${oc4j.dir}/j2ee/home/jazncore.jar"/>
    <pathelement path="${oc4j.dir}/j2ee/home/jazn.jar"/>
    <fileset dir="${oc4j.dir}/j2ee/home/lib/"/>

2. Open eclipse, set the break point in your code, then navigate:

Run->Debug Configuration->Add a new Remote Java application

From the newly created launch configuration, specify the project, host and port for target application, click ‘apply’ and then click ‘debug’ to start debug in Ant.