Java snippet: Debugging a Java library used from Ant

Posted by Michael Studman Fri, 01 Sep 2006 13:05:02 GMT

I recently needed to debug a library which seemed to be misbehaving when used within Ant. After a bit of research here’s the best way I found to debug within Ant that doesn’t require sprinkling your bin/ant or bin\ant.bat with temporary JPDA pixy dust.

Ant doesn’t directly accept the -X arguments needed to set up the JPDA settings so the question is: how do you pass these from a shell or command line? In this, the ANT_OPTS environment variable is your friend. Its contents will be directly passed to the Java process Ant calls.

So here’s how you do it:

Step 1: Set ANT_OPTS to the debug options appropriate for your JVM:

for Java 1.4:

>> export ANT_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8453

for Java 1.5:

>> export ANT_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8453

Step 2: Run ant like normal:

  
>> ant

Step 3: Set your breakpoints.

Step 4: Now attach your debugger.

There might be better ways to do this but this was the least invasive route for me.