How to debug Java code when using ANT script in Ec

2019-01-13 00:34发布

问题:

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:

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.



回答2:

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


回答3:

This is how I got it working for me (Just commenting for future reference).



回答4:

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.



回答5:

This is to help the people who are wondering how to debug the web application that use ant to build and deploy. This is quite frequent in legacy applications. If the project was started as "Dynamic Web Project" as the beginning, following steps and even Ant is not necessary.

Set the break point in your code.

Window -> Show View -> Others -> Servers

Add your server JBoss or Tomcat for example.

Right click on the server and choose 'Debug'.

Make sure that debug="true" is set in ant build file.



回答6:

set ANT_OPTS=%ANT_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432

in Eclipse

Toolbar >> Run >> Debug Configurations >> + >> 

Give the values:

Name: Debug_Ant
Project: active-eclipse-project
Host:localhost
Port:5432


回答7:

Steps:

1)Configure remote java debugger with local host as name, port address as 8000(or whatever your system's port address will be)

2)Create a batch file and keep that in bin folder of your tomcat(this step is required when we want to debug remotely keeping server/s in same system).

in batch file you should keep this line:

set JPDA_ADDRESS=8000

set JPDA_TRANSPORT=dt_socket

catalina.bat jpda start

after this keep a break point in your java code, and debug this remote debugger. it will work.

Happy Coding !!



回答8:

I too faced this problem, I did following steps to resolve.

  1. Put the below lines in ANT file

  2. Go to the debugging configurations->Remote java application-> Create new configuration file with project name,port=5432 and host is localhost and save it.

  3. Now run your build.xml using debugging mode, then you should see in console that "Listening for transport dt_socket at address 5432"

  4. Now run debug configuration file which is you configured. Now your selenium code will run using Debug mode.

Hope this helps.

If you still facing issues, please let me know so that i can help you on that.

Thanks