How to debug Java code when using ANT script in Eclipse

In the <java> ant task you should add two jvm parameters (<jvmarg> IIRC) to turn on debugging:

 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432

This will launch the java program with debugging turned on and the program will be ready to accept debugger connections on port 5432. Then you should use your IDE's remote debugging facility and direct it to connect to port 5432.


In Eclipse:

Toolbar > External Tool Configurations... > (select your existing ANT build or create new) > JRE tab

In "VM Arguments" add:

-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y

Again Toolbar > Debug > Debug Configurations... > Remote Java Application > New

Name: Debug Ant
Project: <Select your project where debug files are kept>
Host: localhost
Port: 8787

Now in "External Tool Configurations" launch "ANT Task" (which waits for the Remote Java Application debugger to connect), then launch the "Debug Ant" from the "Debug" toolbar icon.


(Wasn't able to comment on the given answer, so have to make another answer)

I realized that when launching Ant from Eclipse, you'll have to add fork="true" to the <java> task. Also, it was first not clear to me how to write nested jvmargs, so here goes an example:

<java classname="..." fork="true">
  <jvmarg value="-Xdebug" />
  <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
  ...
</java>