简体   繁体   中英

How to debug Java code when using ANT script in Eclipse

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?!

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

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.

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

Link dump ahead :


Debugging ant tasks is not as simple as plain old java debugging. While you can debug an Ant file adding breakpoints, digging inside the code of specific custom task will require you to add a remote debugger in order to be able to "catch" the running process.

I will explain how to do this in Eclipse, altough I recon it can be achieved with all major java IDEs. First thing is to create a new run configuration for the ant file where you plan to use your customized new task. To do so, go to:

Run -> External Tools -> External Tools configuration...

Right click in Ant Build -> New and in the Main tab select your ant script in Buildfile field. Then go to JRE tab and insert the following JVM arguments:

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

If you wonder what these arguments mean check this, although a bit updated (Java 1.5) still works.

Once this is done, you must create a new Debug configuration for a remote Java application. To do so, navigate to:

Run-> Debug configurations

Drop down the list in the left column and right click in Remote Java Application -> New. Select the project name in the Project field. Default values for host and port are okay as long as you used the same ones for the Ant configuration (JVM arguments).

Everything is ready for the test run! Add breakpoints wherever you consider necessary. In my case, I added one both in the ant script that uses the custom ant task as well as in the custom ant task, in the execute method.

Right click in your ant script or task -> Debug As...-> Ant >Build first

Now BEFORE calling your custom ant task code, go to Run-> Debug Configurations and debug your previously created Java Remote Application config. This will start a separate thread that will debug your custom ant task code, provided that you included some breakpoints :) You can see in the following image how in my case, thread stopped in the execute method of my custom ant task. After this point, it is up to you to decide what to do next...

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM