How to debug Java code when using ANT script in Eclipse

2022-09-01 01:52:39

I have a java class and I need to debug it (put breakpoints and continue using F6). I am using ANT script to init, build, deploy and run the code. I am using:

<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" debuglevel="lines,vars,source">

..........

</javac>

But when I place the breakpoint in a line in my foo.java class and I run the ant script (the run part, Right Click on run-->Debug As-->Ant Build), Eclipse does not stop at that line of code.

What am I missing out?!


答案 1

(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 task. Also, it was first not clear to me how to write nested jvmargs, so here goes an example:<java>

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

答案 2

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

 -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.